Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set/Map symbol vs Obj vs String vs Number
(version: 2)
Comparing performance of:
map add NUMBER vs map add String vs map add Object vs map add Symbol
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = Array.from({length: 1000}).map((_, i) => i); var arrNum = arr.map(v => Number(v)); var arrStr = arr.map(String); var arrObj = arr.map(v=>{return {[v]: v}}) var arrSymb = arr.map(Symbol)
Tests:
map add NUMBER
const m = new Map(); arrNum.forEach(v => m.set(v, v)) arrNum.forEach(v => m.has(v)) arrNum.forEach(v => m.get(v)) arrNum.forEach(v => m.delete(v))
map add String
const m = new Map(); arrStr.forEach(v => m.set(v, v)) arrStr.forEach(v => m.has(v)) arrStr.forEach(v => m.get(v)) arrStr.forEach(v => m.delete(v))
map add Object
const m = new Map(); arrObj.forEach(v => m.set(v, v)) arrObj.forEach(v => m.has(v)) arrObj.forEach(v => m.get(v)) arrObj.forEach(v => m.delete(v))
map add Symbol
const m = new Map(); arrSymb.forEach(v => m.set(v, v)) arrSymb.forEach(v => m.set(v)) arrSymb.forEach(v => m.get(v)) arrSymb.forEach(v => m.delete(v))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
map add NUMBER
map add String
map add Object
map add Symbol
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_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
map add NUMBER
21409.5 Ops/sec
map add String
30689.7 Ops/sec
map add Object
22526.5 Ops/sec
map add Symbol
16947.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its various components. **Benchmark Overview** The test measures the performance of adding elements to a `Map` data structure in JavaScript, comparing different types of values: numbers, strings, objects, and symbols. The goal is to identify which type provides the best performance for this operation. **Options Compared** The options being compared are: 1. **Number**: Regular numbers. 2. **String**: Strings (using `String()` function). 3. **Object**: Objects (using a simple object literal with key-value pairs). 4. **Symbol**: Symbols (created using `Symbol()` function). **Pros and Cons of Each Approach** 1. **Number**: * Pros: Simple and efficient for numeric values. * Cons: May not be optimized for numbers, as they can be represented as doubles in JavaScript. 2. **String**: * Pros: Can be used to represent any value that can be converted to a string. * Cons: May incur additional overhead due to string conversion. 3. **Object**: * Pros: Can be used to represent complex data structures, making it useful for specific use cases. * Cons: May introduce additional overhead due to object creation and property access. 4. **Symbol**: * Pros: Unique and non-replicable values, which can improve performance in certain scenarios. * Cons: Limited support in older browsers and may not be optimized for symbols. **Library/Function Used** In the benchmark, the `Map` data structure is used as the target collection for adding elements. No external libraries are required. **Special JavaScript Feature/Syntax** None are explicitly mentioned or utilized in this benchmark. **Other Considerations** When comparing performance, it's essential to consider factors like: * **Browser and platform support**: Different browsers and platforms may optimize or interpret `Map` operations differently. * **JavaScript engine implementation**: The specific implementation of the JavaScript engine used by the browser can affect performance characteristics. * **Cache and optimization techniques**: Modern JavaScript engines often employ caching and optimization techniques, which can impact performance. **Alternatives** If you're interested in exploring alternative data structures or libraries for performance-critical code, consider: 1. **IndexedDB**: A low-level storage API that provides more control over data access and manipulation. 2. **Set**: Another collection data structure provided by JavaScript, which may offer different performance characteristics compared to `Map`. 3. **External libraries**: Libraries like Lodash or Ramda provide optimized implementations of various data structures and algorithms. Keep in mind that the choice of data structure or library depends on your specific use case, requirements, and constraints.
Related benchmarks:
Array.forEach vs Object.keys().forEach
flatMap vs map/flat
Map.prototype.forEach vs Array.prototype.forEach
Map.forEach vs Array.forEach vs Array.from(Map.prototype.values()).forEach
flat() vs flatMap()
Comments
Confirm delete:
Do you really want to delete benchmark?