Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
key number vs string number
(version: 1)
Comparing performance of:
number as key vs string inter as key
Created:
9 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:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); }
Tests:
number as key
/*When writing async/deferred tests, use `deferred.resolve()` to mark test as done*/ const map1 = new Map(); for (let counter = 0; counter < 1000; counter++) { map1.set(counter, `value_${counter}`); }
string inter as key
const map2 = new Map(); for (let counter = 0; counter < 1000; counter++) { map2.set(`selector_${counter}`, `value_${counter}`); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
number as key
string inter as key
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
number as key
30822.3 Ops/sec
string inter as key
18079.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 9 months ago):
The benchmark being tested compares the performance of using numbers as keys versus using strings as keys in a JavaScript `Map` object. Here’s a breakdown of what is tested, options compared, and other considerations: ### Options Compared 1. **Number as Key**: - **Test Case**: ```javascript const map1 = new Map(); for (let counter = 0; counter < 1000; counter++) { map1.set(counter, `value_${counter}`); } ``` - **Description**: This test uses numbers (`counter`) as keys in a `Map`. The keys range from 0 to 999. 2. **String as Key**: - **Test Case**: ```javascript const map2 = new Map(); for (let counter = 0; counter < 1000; counter++) { map2.set(`selector_${counter}`, `value_${counter}`); } ``` - **Description**: This test uses strings (e.g., `selector_0`, `selector_1`, ..., `selector_999`) as keys in the `Map`. ### Pros and Cons - **Using Numbers as Keys**: - **Pros**: - Potentially faster because numbers can be compared and stored more efficiently than strings; they require less processing overhead. - May consume less memory compared to strings, resulting in better performance during lookups. - **Cons**: - Limited to numeric keys, which might not fit all use cases. - **Using Strings as Keys**: - **Pros**: - More flexible since keys can be descriptive strings, offering better readability and meaning for the key-value pairs. - Suitable for cases where the keys are not strictly numerical and need to represent unique identifiers or names. - **Cons**: - Generally slower performance in comparison to numeric keys, due to the overhead of dealing with string operations, encoding, and comparisons. - Increased memory usage because strings consume more space compared to numbers. ### Alternative Considerations In addition to using `Map`, there are other alternatives and data structures that could be used for key-value storage in JavaScript: 1. **Object**: - Objects are the traditional way to store key-value pairs in JavaScript. However, they do not allow arbitrary keys of any type — keys are always converted to strings. This can lead to unexpected behavior when using non-string keys. 2. **Set**: - While not a direct alternative to `Map` in terms of key-value pairs, `Set` can be used for unique collections of values. However, it does not store key-value relationships. 3. **Using WeakMap**: - A `WeakMap` allows for objects as keys and provides garbage collection for keys that are no longer referenced elsewhere. This is useful for storing private data tied to specific objects. ### Conclusion The benchmark results indicate that the performance of using numbers as keys in a `Map` is distinctly better than using strings, with the respective executions per second being significantly higher for numeric keys (30822.25) compared to string keys (18079.78). This result suggests that when performance is critical and the keys can be represented numerically, using numbers as keys may be the more efficient choice. However, the ultimate decision on which approach to use should also consider the nature of the data and the need for key descriptiveness.
Related benchmarks:
reate array by lenght
Counter Increasement v2
Counter Increasement v4
Counter Increasement v5
Parse number to bigint
js mul vs pow
Test array ops
Array length to string 1
push one by one vs spread
Comments
Confirm delete:
Do you really want to delete benchmark?