Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS Object Creation vs Pooling
(version: 0)
Comparing performance of:
Dynamic Creation vs Pooling
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const n = 10000 var arr = new Array(n).fill(0); for (let i = 0; i < n; i++) { arr[i] = i; }
Tests:
Dynamic Creation
const result = []; for (let i = 0; i < arr.length; i++) { result.push({ x: arr[i], y: arr[i] + 1 }); }
Pooling
const result = []; const obj = { x: 0, y: 0 }; for (let i = 0; i < arr.length; i++) { obj.x = arr[i]; obj.y = arr[i] + 1; result.push(obj); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Dynamic Creation
Pooling
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Dynamic Creation
6563.1 Ops/sec
Pooling
15159.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the JavaScript microbenchmark you provided. **Benchmark Definition** The benchmark tests two approaches to create arrays of objects: 1. **Pooling**: This approach creates an initial array `arr` with 10,000 elements and fills it with zeros. Then, for each element in the array, a new object is created and added to an empty result array. 2. **Dynamic Creation**: In this approach, an empty result array is created initially. For each element in the `arr` array, a new object is created on the fly using the elements from `arr`, and then that object is pushed into the result array. **Options Compared** The benchmark compares two options: * **Pooling**: Creates an initial array of objects and then creates new objects within that array. * **Dynamic Creation**: Creates new objects directly within the result array, without creating a pre-allocated array. **Pros and Cons** * **Pooling**: + Pros: Can be faster because it avoids the overhead of creating a large number of objects on the fly. + Cons: Requires more memory upfront to create the initial array, which may not be desirable in all scenarios. * **Dynamic Creation**: + Pros: Uses less memory upfront and can be more suitable for scenarios where the number of objects is not known beforehand. + Cons: Can be slower because it involves creating new objects on the fly. **Library Usage** There are no libraries explicitly mentioned in this benchmark, but it's worth noting that if a library were used to implement these approaches (e.g., Lodash or Array.prototype.map()), the results might differ due to the additional overhead of the library. **Special JS Features/Syntax** None of the provided benchmark code uses any special JavaScript features or syntax. It only uses basic JavaScript constructs like arrays, loops, and object creation. **Alternative Approaches** Other approaches to creating arrays of objects could include: * **Using `Array.prototype.map()`**: Instead of using a loop to create new objects, you could use the `map()` method to create an array of transformed objects. This approach is often more concise and efficient. * **Using a library like Lodash's `map()` function**: Similar to the previous suggestion, using a library's implementation of `map()` can provide a performance boost compared to the basic JavaScript implementation. Keep in mind that these alternative approaches might have different performance characteristics depending on the specific use case and requirements.
Related benchmarks:
Array fill method vs for loop_
fill vs map
fill vs for loop
for vs new Array.fill v2
TypedArray fill vs loop
Comments
Confirm delete:
Do you really want to delete benchmark?