Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sparse Array Vs. Object,Map,Set, Number Indexes
(version: 4)
Comparing performance of:
Object vs Array vs Set Length Array vs TypedArray vs map vs set
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var iterations =100000; var obj = {}; var arr = []; var map = new Map(); var set = new Set(); var setLengthArr = new Array(iterations); var typedArray = new Uint32Array(iterations);
Tests:
Object
for(let i=0;i<iterations;i+=10){ obj[i]=Math.floor(Math.random()*iterations); obj[i+1]=obj[i]+1; }
Array
for(let i=0;i<iterations;i+=10){ arr[i]=Math.floor(Math.random()*iterations); arr[i+1]= arr[i]+1; }
Set Length Array
for(let i=0;i<iterations;i+=10){ setLengthArr[i]=Math.floor(Math.random()*iterations); setLengthArr[i+1] = setLengthArr[i]+1; }
TypedArray
for(let i=0;i<iterations;i+=10){ typedArray[i]=Math.floor(Math.random()*iterations); typedArray[i+1]= typedArray[i]+1; }
map
for(let i=0;i<iterations;i+=10){ map.set(i,Math.floor(Math.random()*iterations)); map.set(i+1,map.get(i)+1) }
set
for(let i=0;i<iterations;i+=10){ set.add(Math.floor(Math.random()*iterations)); set.add(set.has(i+1)) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Object
Array
Set Length Array
TypedArray
map
set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.6
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object
42294.4 Ops/sec
Array
39973.9 Ops/sec
Set Length Array
58641.1 Ops/sec
TypedArray
62504.4 Ops/sec
map
5964.7 Ops/sec
set
3885.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and its various components, explaining what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares the performance of different data structures in JavaScript: 1. Object 2. Array 3. Set Length Array (a custom array-like structure) 4. TypedArray 5. Map 6. Set Each test case performs a similar operation: assigning a random value to an index and then incrementing it by 1. **Options Compared** The benchmark compares the performance of each data structure in the following scenarios: * Assigning a random value to an index: Object, Array, Set Length Array, TypedArray, Map, and Set * Incrementing the assigned value by 1: Object, Array, Set Length Array, TypedArray, Map **Pros and Cons of Each Approach** Here's a brief overview of each data structure's performance characteristics: * **Object**: Objects are slow due to their dynamic nature. They require creating a new property for each assignment, which incurs additional overhead. + Pros: Easy to implement, flexible + Cons: Slow, may lead to memory issues if not used carefully * **Array**: Arrays are generally faster than objects but still incur some overhead due to indexing and bounds checking. + Pros: Fast, efficient, well-supported in most browsers + Cons: Less flexible than objects, limited methods * **Set Length Array (Custom)**: This is a custom array-like structure that's not part of the standard JavaScript API. Its performance is comparable to arrays but may have issues with edge cases. + Pros: Fast, efficient, can be customized for specific use cases + Cons: Not widely supported, requires manual implementation * **TypedArray**: Typed arrays are a subset of arrays optimized for specific data types (e.g., `Uint32Array`). They're generally faster than regular arrays but may require additional setup. + Pros: Fast, efficient, optimized for specific use cases + Cons: Less flexible than regular arrays, requires additional setup * **Map**: Maps are similar to objects but provide a more efficient way of storing key-value pairs. They're fast and efficient but may incur some overhead due to their dynamic nature. + Pros: Fast, efficient, provides key-value pair functionality + Cons: Less flexible than objects, may lead to memory issues if not used carefully * **Set**: Sets are a collection of unique values. They're generally faster than arrays for insertion and lookup operations but may incur some overhead due to their dynamic nature. + Pros: Fast, efficient, provides fast lookup and insertion operations + Cons: Less flexible than arrays, limited methods **Library/Feature Usage** * None mentioned in the benchmark definition or test cases. **Special JS Feature/Syntax** None mentioned in the benchmark definition or test cases. However, some of the data structures used (e.g., Maps) rely on JavaScript's built-in `Map` and `Set` types, which were introduced in ECMAScript 2015 (ES6). **Alternatives** Other alternatives for testing performance include: * Using a different programming language or framework * Implementing a custom data structure from scratch * Using a benchmarking library like WebPageTest or JSMassive * Utilizing cloud-based benchmarking services Keep in mind that the choice of alternative will depend on the specific requirements and goals of your project.
Related benchmarks:
map vs forEach Chris v2b
iterating from a filled object VS iterating from a map
Array.forEach vs Object.keys().forEach
set vs array iteration 100k elements
3set vs array iteration New doge333
Comments
Confirm delete:
Do you really want to delete benchmark?