Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object Indexed Properties (Read)
(version: 0)
Comparing performance of:
Array vs Object vs Sparse Array vs Sparse Object
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var LIMIT = 10000; var ARRAY = []; var OBJECT = {}; var SPARSE_ARRAY = []; var SPARSE_OBJECT = {}; for (var i = 0; i < LIMIT; i++) { ARRAY[i] = i; OBJECT[i] = i; if (i % 2 === 0) { SPARSE_ARRAY[i] = i; SPARSE_OBJECT[i] = i; } }
Tests:
Array
var total = 0; var target = ARRAY; for (var i = 0; i < LIMIT; i++) { total += target[i]; } return total;
Object
var total = 0; var target = OBJECT; for (var i = 0; i < LIMIT; i++) { total += target[i]; } return total;
Sparse Array
var total = 0; var target = SPARSE_ARRAY; for (var i = 0; i < LIMIT; i++) { if (i in target) { total += target[i]; } } return total;
Sparse Object
var total = 0; var target = SPARSE_OBJECT; for (var i = 0; i < LIMIT; i++) { if (i in target) { total += target[i]; } } return total;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array
Object
Sparse Array
Sparse Object
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):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case, specifically testing the performance of accessing properties using different data structures: arrays and objects (including sparse arrays and sparse objects). **Data Structures Compared** Three types of data structures are compared: 1. **Arrays**: A dense array with 10,000 elements is created. 2. **Objects**: An object with 10,000 key-value pairs is created. 3. **Sparse Arrays and Objects**: Sparse versions of the above data structures are created by only setting values for every other index. **Pros and Cons** Here's a brief overview of each approach: 1. **Arrays**: * Pros: Efficient access to elements using array indexing (`array[i]`). * Cons: May lead to memory waste due to unused indices. 2. **Objects**: * Pros: Allows for flexible data structure with key-value pairs, making it suitable for various use cases. * Cons: Accessing properties using `object[key]` can be slower than array indexing. 3. **Sparse Arrays and Objects**: * Pros: Reduces memory usage by only allocating space for non-zero values. * Cons: May lead to slower performance due to the need to check if a property exists before accessing it (`if (i in target)`). **Library and Special JS Features** None of the provided benchmark test cases use external libraries. However, the `in` operator used in sparse array and object access checks is a special JavaScript feature that allows checking if a property exists on an object. **Other Alternatives** Alternative approaches to testing array and object access performance could include: * Using other data structures like linked lists or trees. * Testing with larger datasets to observe scalability differences. * Incorporating other microbenchmarks, such as string manipulation or DOM manipulation tasks. Keep in mind that the specific test case design and variations may be tailored to focus on certain aspects of array and object access performance.
Related benchmarks:
continuous array vs sparse array
Sparse Array Vs. Object,Map,Set, Number Indexes
SparseSet - object literal vs class
Sparse Array Vs. Object, Map, Set, Number Indexes 2
Comments
Confirm delete:
Do you really want to delete benchmark?