Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
literal, literal spread, vs reuse
(version: 0)
Comparing performance of:
literal spread vs reuse vs literal
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
arr = []; for (let i = 0; i < 10000; i++) { arr.push({ a: Math.random(), b: Math.random(), c: Math.random() }) } total = 0;
Tests:
literal spread
const k = Math.random(); total += arr.map((obj) => { return { ...obj, d: obj.a + obj.b + k, }; }).reduce((total, obj) => { return total + obj.d / obj.c; }, 0);
reuse
const k = Math.random(); const r = { a: 0, b: 0, c: 0, d: 0, }; total += arr.map((obj) => { r.a = obj.a; r.b = obj.b; r.c = obj.c; r.d = obj.a + obj.b + k; return r; }).reduce((total, obj) => { return total + obj.d / obj.c; }, 0);
literal
const k = Math.random(); total += arr.map((obj) => { return { a: obj.a, b: obj.b, c: obj.c, d: obj.a + obj.b + k, }; }).reduce((total, obj) => { return total + obj.d / obj.c; }, 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
literal spread
reuse
literal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
literal spread
1830.0 Ops/sec
reuse
5338.9 Ops/sec
literal
3145.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of three approaches: 1. Literal 2. Reuse 3. Literal Spread These approaches differ in how they construct objects and perform calculations on them. **Approach 1: Literal** In this approach, a new object is created for each iteration using the `const r = { ... }` syntax. The properties of the object are set individually using dot notation (`r.a = obj.a`, `r.b = obj.b`, etc.). This approach creates a new object for every iteration, which can lead to memory allocation overhead. **Approach 2: Reuse** In this approach, a single object `r` is created outside the loop and its properties are updated within the loop. The `map()` function returns an array of objects, but in this case, it's not being used as intended. Instead, each iteration creates a new object by updating the existing `r` object with new values from the `obj` array. This approach can lead to confusion and potential errors. **Approach 3: Literal Spread** In this approach, the spread operator (`{ ... }`) is used to create a shallow copy of the `obj` object. The `map()` function returns an array of objects with the new properties added (in this case, `d`). This approach creates a new object for every iteration without modifying the original `obj` array. **Library and Special Features** There are no external libraries or special features used in these benchmark definitions. **Considerations** When choosing an approach, consider the following factors: * Performance: Creating objects with dot notation can lead to slower performance due to memory allocation overhead. * Code readability and maintainability: The `reuse` approach can be confusing if not implemented correctly. The `literal spread` approach is often preferred for its conciseness and readability. **Other Alternatives** If you're interested in exploring alternative approaches, consider the following: * Using a library like Lodash or Ramda to simplify object manipulation. * Utilizing `Array.prototype.reduce()` with an initial value instead of accumulating totals manually. * Experimenting with other data structures, such as using arrays instead of objects. Keep in mind that these alternatives might not be directly relevant to the specific problem being solved but can provide insights into different programming paradigms and techniques.
Related benchmarks:
Fill array with random integers
Preinitialized array size vs Push operations to an empty one.
Array .push() vs .unshift() with random numbers
Array push vs
Array push or set
Comments
Confirm delete:
Do you really want to delete benchmark?