Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
return or null
(version: 1)
Comparing performance of:
getAsIs vs getOrNull
Created:
10 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ const m = new Map([[1, 1],[2,2],[3,3]]); const getAsIs = (key) => m.get(key); const getOrNull = (key) => m.get(key) ?? null; const test = (t) => { for (let index = 0; index < 100000; index++) { t(index); } } async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); }
Tests:
getAsIs
test(getAsIs);
getOrNull
test(getOrNull);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
getAsIs
getOrNull
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
getAsIs
39500.0 Ops/sec
getOrNull
2227.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 10 months ago):
The benchmark defined in the provided JSON assesses the performance of two different JavaScript functions that retrieve values from a Map data structure: `getAsIs` and `getOrNull`. Here’s a detailed breakdown of what each function does and how they compare: ### Functions Defined: 1. **`getAsIs`** - **Definition**: This function takes a key as an argument and retrieves the corresponding value from the Map `m`. If the key does not exist in the Map, it will return `undefined`. - **Performance**: This function is straightforward in its operation—directly checking for the presence of a key in the Map and retrieving associated values. 2. **`getOrNull`** - **Definition**: Similar to `getAsIs`, this function retrieves the value for the given key. However, if the key does not exist, instead of returning `undefined`, it returns `null` using the nullish coalescing operator (`??`), which was introduced in ES2020. - **Performance**: This function adds an additional step to convert `undefined` to `null`, which may incur a slight performance overhead. ### Benchmark Purpose: The benchmark compares the execution performance of the two functions across a large number of calls (100,000 in this case). The results show how many times each function can be executed per second, providing insight into their efficiency under similar conditions. ### Test Results: - For `getAsIs`: The function executed **39,500 times per second**. - For `getOrNull`: The function executed **2,227 times per second**. ### Pros and Cons: - **getAsIs**: - **Pros**: Generally faster performance, as it performs a simple retrieval without additional checks or transformations. - **Cons**: Returns `undefined` for absent keys, which may not be semantically appropriate in all contexts where a null value may be preferred. - **getOrNull**: - **Pros**: Offers a clearer semantic representation when a key is absent by returning `null`, which can help in differentiating between "not found" and "found with an empty value". - **Cons**: Slower performance due to extra overhead of checking and returning null instead of undefined. ### Other Considerations: 1. **Performance Impact**: Depending on the application, the performance difference may or may not be significant. In a case where such functions are called frequently in a performance-critical section of code, the faster function (`getAsIs`) may be more appropriate. 2. **Code Readability**: Code readability and maintainability should also be considered. Developers should pick the function that best fits their semantic requirements in addition to performance. 3. **Alternatives**: Alternatives to using `Map` and these specific functions include: - Using plain objects for key-value storage. However, plain objects can have prototype properties that may lead to unintended behavior unless handled properly. - Utilizing other data structures like Sets if only the presence of keys is needed without associated values. Overall, this benchmark offers valuable insight into the performance trade-offs between two approaches for value retrieval in JavaScript, helping developers make informed decisions based on their specific use cases and performance requirements.
Related benchmarks:
Pull from JS
Access Object, Map, Set
test Map vs Object with string which use string as key and value
Nullish vs If
Nullish vs If 2 lvl
Object vs Map MYTEST2137
Object vs Map MYTEST21372
Array length to string 1
Map vs Object - Insertion, Lookup & Update
Comments
Confirm delete:
Do you really want to delete benchmark?