Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
10000 Object Pool; Array vs Map Memory V3
(version: 2)
Comparing array find vs map get
Comparing performance of:
Array Memory vs Map Memory
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var objectPoolSize = 10000 var randomMax = 10000000 const getRandomNumber = (max) => { return Math.floor(Math.random() * max); } function getRandomString() { return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5) + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5) + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5) + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5) } class TestObject { constructor() { this.a = getRandomString() this.b = 'Test String' this.c = 'Other Random String asdasdasdkjasdljkasdjkl;asjk;ldasjk;ldasjkldjas' this.d = ['asdasdsadsad', 'asdfaqweqweasdsasd', 'klsdjxcvcopxjasddsa', 'qwoepopizxzciomnas,mdsalikhj'] } } var createObject = () => { return new TestObject() } var objectPool = [] for (let i = 0; i < objectPoolSize; i++) { objectPool.push(createObject()) }
Tests:
Array Memory
const array = [] for (let i = 0; i < objectPool.length; i++) { array.push(objectPool[i]) }
Map Memory
const map = new Map() for (let i = 0; i < objectPool.length; i++) { map.set(objectPool[i].a, objectPool[i]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array Memory
Map Memory
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):
Let's break down the provided JSON and explain what is tested, compared, and their pros/cons. **Benchmark Definition** The benchmark defines two test cases: 1. **Array Memory**: The script creates an array `array` by pushing objects from `objectPool` into it. The purpose of this test case is to measure the memory usage or overhead associated with using an array for storing objects. 2. **Map Memory**: Similar to the first test case, but instead of using an array, a new `Map` object is created and each object from `objectPool` is added as a key-value pair. **What are compared?** Two approaches are being compared: * Using an array (`array`) for storing objects * Using a Map object (`map`) for storing objects **Pros/Cons of different approaches** 1. **Array**: * Pros: Arrays are lightweight, easy to use, and provide direct access to elements. * Cons: Arrays can lead to slower performance due to the need to iterate over each element, which might cause a lot of memory allocation and deallocation. 2. **Map**: * Pros: Maps provide fast lookups and allow for efficient storage and retrieval of objects using keys. This can reduce memory usage compared to arrays. * Cons: Maps require more memory overhead due to the need to store key-value pairs, which might increase memory fragmentation. **Library/ Framework** The benchmark uses a custom `TestObject` class with properties like `a`, `b`, and `d`. This class is used to generate random strings and arrays for testing purposes. The `objectPoolSize` variable determines how many objects are generated and stored in the `objectPool`. **Special JS feature or syntax** None mentioned. **Other alternatives** If the benchmark results show that either approach has a significant performance advantage, other alternatives could include: 1. **Using a different data structure**, such as an object (with no keys) or a custom data structure designed for fast storage and retrieval. 2. **Using caching mechanisms**, like memoization or caching libraries, to reduce memory allocation and deallocation overhead. In summary, the benchmark tests two common JavaScript approaches for storing objects: arrays and Map objects. The results will help determine which approach has better performance characteristics in terms of memory usage or overhead, depending on the specific use case.
Related benchmarks:
for vs foreach vs for..of, but with rich object array, but fair
1000 Objects in a 10000 Object Pool; Array find vs Map get
10 Objects in a 100 Object Pool; Array find vs Map get
1000 Objects in a 10000 Object Pool; Array find vs Map get - using string keys
Comments
Confirm delete:
Do you really want to delete benchmark?