Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map string versus number keys
(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(); intMap.set(1, "aaa"); intMap.set(2, "bbb"); intMap.set(3, "ccc"); var stringMap = new Map(); stringMap.set("a", "aaa"); stringMap.set("b", "bbb"); stringMap.set("c", "ccc");
Tests:
String
var a = stringMap.get("a"); var b = stringMap.get("b"); var c = stringMap.get("c");
Int
var a1 = intMap.get(1); var b2 = intMap.get(2); var c3 = intMap.get(3);
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; rv:109.0) Gecko/20100101 Firefox/115.0
Browser/OS:
Firefox 115 on Mac OS X 10.12
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String
939031552.0 Ops/sec
Int
957639296.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is defined in JSON format, which represents a test case for JavaScript microbenchmarks on MeasureThat.net. The definition includes: * `Name` and `Description`: These fields provide metadata about the benchmark, but they're empty in this case. * `Script Preparation Code`: This code initializes two Maps: `intMap` and `stringMap`. The maps are populated with key-value pairs using integers and strings respectively. This setup ensures that both Map types can be used for testing. * `Html Preparation Code`: There is no HTML preparation code provided, so we'll focus on the JavaScript implementation. **Individual Test Cases** The benchmark consists of two test cases: 1. **String**: This test case uses the `stringMap` to retrieve values using the `.get()` method. 2. **Int**: This test case uses the `intMap` to retrieve values using the `.get()` method with an integer key. **What's being tested?** The benchmark tests the performance difference between accessing keys in a Map using integers and strings. The goal is to measure which approach (integer-based or string-based) yields better performance. **Comparison options** There are two primary approaches compared: 1. **Integer-based**: Using the `.get()` method with an integer key (e.g., `intMap.get(1)`). 2. **String-based**: Using the `.get()` method with a string key (e.g., `stringMap.get("a")`). **Pros and Cons of each approach** **Integer-based:** Pros: * Typically faster, as JavaScript's internal integer representation is usually smaller than a string. * Can be more cache-friendly, depending on the implementation. Cons: * May not work correctly if you're using a large range of keys (e.g., negative numbers or very high integers). **String-based:** Pros: * More flexible, as strings can represent any value, making it easier to store and retrieve arbitrary data. * Works with key names that are more human-readable. Cons: * Typically slower than integer-based access due to the overhead of string concatenation and lookup in a hash table. **Library usage** In this benchmark, no external libraries are used beyond the built-in JavaScript `Map` object. However, some JavaScript engines might use optimizations or implementations that could affect performance (e.g., some engines use a custom hash function). **Special JS feature or syntax** This benchmark doesn't use any special JavaScript features or syntax. **Other alternatives** If you're interested in exploring alternative approaches or test cases, here are a few examples: * Using other data structures like `Set` or `Array` for testing. * Adding additional keys to the Map (e.g., with different key types) to simulate more realistic scenarios. * Incorporating external dependencies or third-party libraries to introduce potential bottlenecks. Keep in mind that these alternatives might affect the benchmark's accuracy, and it's essential to carefully consider the test case goals and requirements when exploring new options.
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 big sets
Comments
Confirm delete:
Do you really want to delete benchmark?