Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map string versus number keys big sets
(version: 0)
Comparing performance of:
String vs Int
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var intMap = new Map(); for(i=0; i < 1000; i++){ intMap.set(i, "a" + i); } var stringMap = new Map(); for(i=0; i < 1000; i++){ intMap.set(i.toString(), "a" + i); }
Tests:
String
for(i=0; i < 1000; i++){ var a = stringMap.get(i.toString()); }
Int
for(i=0; i < 1000; i++){ var a = stringMap.get(i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String
Int
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.2 Safari/605.1.15
Browser/OS:
Safari 12 on Mac OS X 10.12.6
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String
22664.0 Ops/sec
Int
1606028.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark measures the performance of JavaScript maps when using string versus number keys, particularly with large sets. The script preparation code creates two maps: `intMap` with integer keys (0 to 999) and `stringMap` with concatenated string keys ("a" + i). **Options Compared** Two approaches are compared: 1. **String Keys**: The benchmark tests the execution speed of retrieving values from the `stringMap`. In this case, the key is a concatenation of "a" and the integer value. 2. **Number Keys**: The benchmark tests the execution speed of retrieving values from the `intMap`, which has integer keys. **Pros and Cons** **String Keys (stringMap)** Pros: * Easier to read and maintain code, as strings are more human-readable * Can be beneficial for situations where the key is a meaningful string representation of the value Cons: * May lead to slower performance due to the overhead of string concatenation and hashing * May result in slower lookup times compared to integer keys **Number Keys (intMap)** Pros: * Typically faster performance due to optimized integer hashing and comparison algorithms * Can lead to better cache locality, as consecutive integers tend to be stored together in memory Cons: * Less readable code, especially for developers unfamiliar with the internal workings of JavaScript's map implementation * May require more complex data structures or workarounds for certain use cases **Library Usage** In this benchmark, no external libraries are explicitly used. However, it's worth noting that modern JavaScript engines often include optimized implementations of various data structures and algorithms, which can affect performance. **Special JS Features or Syntax** This benchmark does not utilize any special JavaScript features or syntax beyond standard ECMAScript 2020 features. It only relies on the built-in `Map` data structure and basic iteration constructs (e.g., `for` loops). **Other Alternatives** To measure similar performance characteristics, other benchmarks might use: * Array vs. Map: A benchmark comparing the performance of JavaScript arrays versus maps. * Object vs. Map: A benchmark comparing the performance of JavaScript objects versus maps. * Custom data structures: A benchmark using custom, hand-rolled data structures to simulate map-like behavior. Keep in mind that the choice of alternative benchmark will depend on the specific use case and performance characteristics being measured.
Related benchmarks:
Map vs Object Improved
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
Map string versus number keys
Comments
Confirm delete:
Do you really want to delete benchmark?