Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
int vs str map key perf set and get 1
(version: 1)
Comparing performance of:
int vs str
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
var intMap = new Map(); var stringMap = new Map();
Tests:
int
intMap.set(1, "aaa"); intMap.set(2, "bbb"); intMap.set(3, "ccc"); var a1 = intMap.get(1); var b2 = intMap.get(2); var c3 = intMap.get(3);
str
stringMap.set("1", "aaa"); stringMap.set("2", "bbb"); stringMap.set("3", "ccc"); var a = stringMap.get("1"); var b = stringMap.get("2"); var c = stringMap.get("3");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
int
str
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:137.0) Gecko/20100101 Firefox/137.0
Browser/OS:
Firefox 137 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
int
16325349.0 Ops/sec
str
14905900.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark titled "int vs str map key perf set and get 1" compares the performance of two different types of keys used in a JavaScript `Map`: integer keys and string keys. The benchmark specifically measures how fast these two different types of keys can be set and retrieved from the map. ### Benchmark Overview - **Testing Options:** - **Integer Keys (`intMap`)**: Uses integer values as keys (e.g., 1, 2, 3). - **String Keys (`stringMap`)**: Uses string representation of numbers as keys (e.g., "1", "2", "3"). ### Test Cases Explained 1. **Integer Key Test (`intMap`)**: - The code sets three values in a `Map` using integers as keys (1, 2, 3), and then retrieves these values using the same integer keys. - This test measures the performance in terms of how many operations can be executed per second when using integers. 2. **String Key Test (`stringMap`)**: - Similar to the integer test, this one uses string keys ("1", "2", "3") to set and retrieve values in a `Map`. - This evaluates the performance of string keys in terms of operations per second. ### Performance Results From the benchmark results: - **Integer Keys (`intMap`)**: - Executions per second: **16,325,349.0** - **String Keys (`stringMap`)**: - Executions per second: **14,905,900.0** Here, it is clear that integer keys outperform string keys by a significant margin (about 9.5% faster in this instance). ### Pros and Cons of Using Integer vs. String Keys - **Integer Keys**: - **Pros**: - Generally faster for retrieving values since they require less processing compared to strings (no type coercion or string comparison). - More efficient memory usage, as integers generally consume less space. - **Cons**: - Limited to numeric keys, which may not fit all use cases. - **String Keys**: - **Pros**: - More flexible, allowing non-numeric keys to be used in `Map`. - Useful if keys need to be represented in human-readable format or when including symbols. - **Cons**: - Slower performance due to additional overhead in processing string comparisons and conversions. - Higher memory consumption compared to integers. ### Alternative Approaches When considering `Map` with either integer or string keys, there are several alternatives to consider: 1. **Plain Objects**: - Objects (`{}`) can also be used as key-value stores in JavaScript. However, they only support string keys, and Integer keys would be implicitly converted to strings. - Pros: simpler syntax for accessing properties. - Cons: lacks built-in methods of `Map` (like `size`) and does not maintain key order. 2. **Arrays**: - For cases where keys are sequential numbers, arrays can be used. However, arrays do not provide efficient lookups like `Map`. - Pros: Ideal for index-based data and simpler syntactic sugar. - Cons: Inefficient for searching non-sequential or non-indexed values. 3. **WeakMap**: - Useful when keys should not prevent garbage collection of the objects. This helps in managing memory more effectively in certain contexts. - Pros: Memory efficient for temporary references or caching. - Cons: No size property and cannot enumerate keys. In conclusion, this benchmark helps identify performance trade-offs in using different types of keys in a `Map`. While integer keys generally deliver better performance, string keys offer flexibility that may be necessary for certain applications. Decisions about which key type to use should consider both performance and the nature of the data being stored.
Related benchmarks:
Foreach vs Spread
Map vs Object Improved
Map vs Object setup 1
Map vs Object: Full benchmark
Map string versus number keys
Map string versus number keys big sets
Map.has vs Object hasOwnProperty
Map.has vs Object hasOwnProperty vs direct check v2
int vs str map key perf set and get
Comments
Confirm delete:
Do you really want to delete benchmark?