Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Stor info in Array vs Object
(version: 0)
Comparing performance of:
Array index vs Object vs Array.push
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function makeid(length) { var result = ''; var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var charactersLength = characters.length; for ( var i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } var values = Array.from({length: 1000}).map(()=> makeid(10));
Tests:
Array index
let a = []; for(let i = 0; i < values.length; i++) a[i] = values[i];
Object
let b = {}; for(let i = 0; i < values.length; i++) b[values[i]] = values[i];
Array.push
let c = []; for(let i = 0; i < values.length; i++) c.push(values[i]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array index
Object
Array.push
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is designed to compare three approaches for storing information in JavaScript: 1. **Array**: Storing values in an array using `values[i]`. 2. **Object**: Storing values as keys in an object using `b[values[i]] = values[i]`. 3. **Array.push**: Storing values in an array using the `push` method. **Options Compared** The benchmark compares the performance of these three approaches for storing information in JavaScript. The key factors being measured are: * **Memory allocation**: How much memory is allocated by each approach? * **Execution time**: How long does it take to execute each approach? **Pros and Cons of Each Approach** 1. **Array**: * Pros: Simple, efficient, and widely supported. * Cons: Not suitable for large amounts of data, as arrays have a fixed size. 2. **Object**: * Pros: Suitable for large amounts of data, as objects can grow dynamically. Also, provides faster lookups than array indices. * Cons: Requires more memory allocation, and some older browsers may not support it properly. 3. **Array.push**: * Pros: Efficient for adding new elements to the end of an array. * Cons: Not suitable for storing large amounts of data in a single array. **Library Used** In this benchmark, no external libraries are used. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax being tested in this benchmark. It only involves basic language features like arrays and objects. **Benchmark Preparation Code** The script preparation code generates an array of 1000 random strings (each 10 characters long) using the `makeid` function, which is not a standard JavaScript function. This allows the benchmark to test performance with large datasets. **Individual Test Cases** There are three test cases: 1. **Array index**: Tests storing values in an array using `values[i]`. 2. **Object**: Tests storing values as keys in an object using `b[values[i]] = values[i]`. 3. **Array.push**: Tests adding new elements to the end of an array using `c.push(values[i])`. **Latest Benchmark Result** The latest benchmark result shows the performance metrics for each test case: | TestName | ExecutionsPerSecond | | --- | --- | | Array index | 6392.154296875 | | Array.push | 5795.93310546875 | | Object | 3136.933349609375 | These results indicate that: * Storing values in an array using `values[i]` is the fastest approach. * Adding new elements to an array using `push` is faster than storing values as keys in an object. Keep in mind that these results are specific to this benchmark and may not be representative of real-world performance.
Related benchmarks:
Lodash _.some vs _.includes vs array.find
Lodash vs vanila 2
Lodash sort vs array.prototype.sort for objects with strings
Lodash difference vs JS filter and includes with big array
array find() vs js object
Comments
Confirm delete:
Do you really want to delete benchmark?