{"id":16485199,"url":"https://github.com/antoniofalcaojr/benchmark","last_synced_at":"2026-03-03T21:31:12.571Z","repository":{"id":45045688,"uuid":"336140798","full_name":"AntonioFalcaoJr/Benchmark","owner":"AntonioFalcaoJr","description":"Sometimes, you just need to check whether a collection is empty or null to skip execution. But how expensive is that?","archived":false,"fork":false,"pushed_at":"2023-02-20T08:58:37.000Z","size":23,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-29T22:37:03.981Z","etag":null,"topics":["benchmark","dotnet","dotnet-core","dotnet5"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AntonioFalcaoJr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-05T02:26:06.000Z","updated_at":"2023-08-24T07:18:37.000Z","dependencies_parsed_at":"2025-02-14T11:16:38.068Z","dependency_job_id":null,"html_url":"https://github.com/AntonioFalcaoJr/Benchmark","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/AntonioFalcaoJr/Benchmark","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AntonioFalcaoJr%2FBenchmark","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AntonioFalcaoJr%2FBenchmark/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AntonioFalcaoJr%2FBenchmark/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AntonioFalcaoJr%2FBenchmark/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AntonioFalcaoJr","download_url":"https://codeload.github.com/AntonioFalcaoJr/Benchmark/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AntonioFalcaoJr%2FBenchmark/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30062344,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-03T18:21:05.932Z","status":"ssl_error","status_checked_at":"2026-03-03T18:20:59.341Z","response_time":61,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["benchmark","dotnet","dotnet-core","dotnet5"],"created_at":"2024-10-11T13:24:34.903Z","updated_at":"2026-03-03T21:31:12.552Z","avatar_url":"https://github.com/AntonioFalcaoJr.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Benchmark\r\n\r\nSometimes, you just need to check whether a collection is empty or null to skip execution. But how expensive is that?\r\n\r\nThis simple benchmark tested some ways to do this:\r\n\r\n## Scenarios\r\n\r\n```c#\r\n// SCENARIO 1\r\n\r\n    var list = Ids?.ToList() ?? new List\u003cGuid\u003e();\r\n    if (list.Any() is false) { }\r\n\r\n\r\n// SCENARIO 2\r\n\r\n    var list = Ids?.ToArray() ?? Array.Empty\u003cGuid\u003e();\r\n    if (list.Any() is false) { }\r\n\r\n\r\n// SCENARIO 3\r\n\r\n    // toList(); \r\n    var list = _ids?.ToList();\r\n    if (list is null || list.Any() is false) { }\r\n    // toArray();\r\n    var list = _ids?.ToArray();\r\n    if (list is null || list.Any() is false) { }\r\n\r\n\r\n// SCENARIO 4\r\n\r\n    // toList();\r\n    var list = Ids?.ToList();\r\n    if (list is {Count: \u003e 0} is false) { }\r\n    // toArray();\r\n    var list = _ids?.ToArray();\r\n    if (list is {Length: \u003e 0} is false) { }\r\n\r\n\r\n// SCENARIO 5\r\n\r\n    // toList();\r\n    var list = _ids?.ToList();\r\n    if (list is not {Count: \u003e 0}) { }\r\n    // toArray();\r\n    var list = _ids?.ToArray();\r\n    if (list is not {Length: \u003e 0}) { }\r\n\r\n\r\n// SCENARIO 6\r\n\r\n    // toList();\r\n    var list = Ids?.ToList();\r\n    if (list?.Count \u003c= 0) { }\r\n    // toArray();\r\n    var list = _ids?.ToArray();\r\n    if (list?.Length \u003c= 0) { }\r\n```\r\n\r\n### Result\r\n\r\n``` ini\r\nBenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.1198 (1909/November2018Update/19H2)\r\nIntel Core i7-9700 CPU 3.00GHz, 1 CPU, 8 logical and 8 physical cores\r\n.NET Core SDK=5.0.103\r\n  [Host]        : .NET Core 5.0.3 (CoreCLR 5.0.321.7212, CoreFX 5.0.321.7212), X64 RyuJIT\r\n  .NET Core 3.1 : .NET Core 3.1.12 (CoreCLR 4.700.21.6504, CoreFX 4.700.21.6905), X64 RyuJIT\r\n  .NET Core 5.0 : .NET Core 5.0.3 (CoreCLR 5.0.321.7212, CoreFX 5.0.321.7212), X64 RyuJIT\r\n```\r\n|            Method |       Runtime |       Mean |     Error |    StdDev | Ratio | RatioSD |\r\n|------------------ |-------------- |-----------:|----------:|----------:|------:|--------:|\r\n|         Scenario1 | .NET Core 3.1 | 22.2840 ns | 0.2214 ns | 0.2071 ns |  2.55 |    0.04 |\r\n|         Scenario1 | .NET Core 5.0 |  8.7312 ns | 0.1323 ns | 0.1238 ns |  1.00 |    0.00 |\r\n|                   |               |            |           |           |       |         |\r\n|                   |               |            |           |           |       |         |\r\n|         Scenario2 | .NET Core 3.1 |  7.7519 ns | 0.1216 ns | 0.1078 ns |  0.90 |    0.01 |\r\n|         Scenario2 | .NET Core 5.0 |  8.5893 ns | 0.0382 ns | 0.0357 ns |  1.00 |    0.00 |\r\n|                   |               |            |           |           |       |         |\r\n|                   |               |            |           |           |       |         |\r\n|  Scenario3_toList | .NET Core 3.1 |  0.5135 ns | 0.0123 ns | 0.0115 ns |  1.01 |    0.03 |\r\n|  Scenario3_toList | .NET Core 5.0 |  0.5075 ns | 0.0134 ns | 0.0105 ns |  1.00 |    0.00 |\r\n|                   |               |            |           |           |       |         |\r\n|                   |               |            |           |           |       |         |\r\n| Scenario3_toArray | .NET Core 3.1 |  0.5193 ns | 0.0127 ns | 0.0119 ns |  0.64 |    0.03 |\r\n| Scenario3_toArray | .NET Core 5.0 |  0.8007 ns | 0.0188 ns | 0.0293 ns |  1.00 |    0.00 |\r\n|                   |               |            |           |           |       |         |\r\n|                   |               |            |           |           |       |         |\r\n|  Scenario4_toList | .NET Core 3.1 |  0.2388 ns | 0.0053 ns | 0.0049 ns |  0.85 |    0.05 |\r\n|  Scenario4_toList | .NET Core 5.0 |  0.2836 ns | 0.0185 ns | 0.0155 ns |  1.00 |    0.00 |\r\n|                   |               |            |           |           |       |         |\r\n|                   |               |            |           |           |       |         |\r\n| Scenario4_toArray | .NET Core 3.1 |  0.4935 ns | 0.0115 ns | 0.0102 ns |  0.98 |    0.02 |\r\n| Scenario4_toArray | .NET Core 5.0 |  0.5018 ns | 0.0090 ns | 0.0075 ns |  1.00 |    0.00 |\r\n|                   |               |            |           |           |       |         |\r\n|                   |               |            |           |           |       |         |\r\n|  Scenario5_toList | .NET Core 3.1 |  0.2488 ns | 0.0094 ns | 0.0079 ns |  1.80 |    0.19 |\r\n|  **Scenario5_toList** | **.NET Core 5.0** |  **0.1400 ns** | **0.0149 ns** | **0.0132 ns** |  **1.00** |    **0.00** |\r\n|                   |               |            |           |           |       |         |\r\n|                   |               |            |           |           |       |         |\r\n| Scenario5_toArray | .NET Core 3.1 |  0.5848 ns | 0.0342 ns | 0.0320 ns |  0.83 |    0.05 |\r\n| Scenario5_toArray | .NET Core 5.0 |  0.7082 ns | 0.0160 ns | 0.0142 ns |  1.00 |    0.00 |\r\n|                   |               |            |           |           |       |         |\r\n|                   |               |            |           |           |       |         |\r\n|  Scenario6_toList | .NET Core 3.1 |  0.2231 ns | 0.0071 ns | 0.0066 ns |  0.98 |    0.06 |\r\n|  Scenario6_toList | .NET Core 5.0 |  0.2282 ns | 0.0110 ns | 0.0103 ns |  1.00 |    0.00 |\r\n|                   |               |            |           |           |       |         |\r\n|                   |               |            |           |           |       |         |\r\n| Scenario6_toArray | .NET Core 3.1 |  0.6167 ns | 0.0390 ns | 0.0365 ns |  1.12 |    0.10 |\r\n| Scenario6_toArray | .NET Core 5.0 |  0.5741 ns | 0.0386 ns | 0.0516 ns |  1.00 |    0.00 |\r\n\r\n### Hints\r\n\r\n#### Outliers\r\n\r\n```ini\r\nScenarios.Scenario2: .NET Core 3.1         -\u003e 1 outlier  was  removed (10.01 ns)\r\nScenarios.Scenario3_toList: .NET Core 5.0  -\u003e 3 outliers were removed (1.99 ns..2.12 ns)\r\nScenarios.Scenario3_toArray: .NET Core 5.0 -\u003e 9 outliers were removed (2.37 ns..2.44 ns)\r\nScenarios.Scenario4_toList: .NET Core 5.0  -\u003e 2 outliers were removed (1.77 ns, 1.77 ns)\r\nScenarios.Scenario4_toArray: .NET Core 3.1 -\u003e 1 outlier  was  removed (1.97 ns)\r\nScenarios.Scenario4_toArray: .NET Core 5.0 -\u003e 2 outliers were removed (1.96 ns, 1.99 ns)\r\nScenarios.Scenario5_toList: .NET Core 3.1  -\u003e 2 outliers were removed (1.73 ns, 1.75 ns)\r\nScenarios.Scenario5_toList: .NET Core 5.0  -\u003e 1 outlier  was  removed (1.60 ns)\r\nScenarios.Scenario5_toArray: .NET Core 5.0 -\u003e 1 outlier  was  removed, 3 outliers were detected (2.04 ns, 2.05 ns, 2.10 ns)\r\nScenarios.Scenario6_toArray: .NET Core 5.0 -\u003e 2 outliers were removed, 7 outliers were detected (1.85 ns..1.86 ns, 2.05 ns, 2.08 ns)\r\n```\r\n---\r\n\r\n### Legends\r\n\r\n```ini\r\nMean    : Arithmetic mean of all measurements\r\nError   : Half of 99.9% confidence interval\r\nStdDev  : Standard deviation of all measurements\r\nRatio   : Mean of the ratio distribution ([Current]/[Baseline])\r\nRatioSD : Standard deviation of the ratio distribution ([Current]/[Baseline])\r\n1 ns    : 1 Nanosecond (0.000000001 sec)\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantoniofalcaojr%2Fbenchmark","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantoniofalcaojr%2Fbenchmark","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantoniofalcaojr%2Fbenchmark/lists"}