Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread it and Assign Again
(version: 0)
Comparing performance of:
map vs empty assign vs data assign vs in place mod
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = { 'one': [{ foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } } ], 'two': [{ foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { replace: "yes", bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } }, { foo: { bar: { baz: "hi" } } } ] };
Tests:
map
let f = { ...data, two: data.two.map((item) => { item.foo.replace == "yes" ? { ...item, something: { n: 234, b: 234 } } : item }), };
empty assign
let o = Object.assign({}, data, { two: data.two.map((item) => { item.foo.replace == "yes" ? { ...item, something: { n: 234, b: 234 } } : item }) })
data assign
let o = Object.assign(data, { two: data.two.map((item) => item.foo.replace == "yes" ? { ...item, something: { n: 234, b: 234 } } : item ) });
in place mod
let f = data.two.find((item) => item.foo.replace == "yes"); f.n = 234; f.b = 234; data
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
map
empty assign
data assign
in place mod
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 benchmark and explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Overview** The test case "Spread it and Assign Again" uses an object `data` with nested properties to demonstrate the performance of different JavaScript methods for modifying objects. The main objective is to compare the execution speed of various approaches that achieve the same result. **Benchmark Definition JSON** The benchmark definition includes: * A script preparation code: This defines the `data` object, which contains multiple levels of nesting. * Individual test cases: * "map" (4 iterations) * "empty assign" (1 iteration) * "data assign" (1 iteration) * "in place mod" (1 iteration) **Test Cases** Each test case uses a different method to modify the `data` object: * **"map"`: Uses the `Array.prototype.map()` method to create a new array with modified objects. * **"empty assign"`: Uses `Object.assign({}, data, { ... })` to merge two objects and create a new one. * **"data assign"`: Uses `Object.assign(data, { ... })` to merge two objects into the existing `data` object. * **"in place mod"`: Directly modifies individual objects within the nested structure. **Options Compared** The benchmark compares the execution speed of four different methods: 1. **`map()`**: Creates a new array with modified objects, which can be slower due to the overhead of creating a new array and iterating over each element. 2. **"empty assign"`: Merges two objects using `Object.assign`, which creates a new object and copies properties from one object into another. 3. **"data assign"`: Merges two objects into an existing object, also using `Object.assign`. This approach modifies the original data in-place. 4. **"in place mod"`: Directly modifies individual objects within the nested structure. **Pros and Cons** * **`map()`**: Pros: * More concise code * Encapsulates modification logic within a single function call Cons: * Creates a new array, which can be slower due to memory allocation and garbage collection. * **"empty assign"`: Pros: * Easy to understand and implement Cons: * Creates a new object, which incurs additional overhead. * **"data assign"`: Pros: * Modifies the original data in-place Cons: * Can be less readable due to nested `Object.assign()` calls * **"in place mod"`: Pros: * Fastest execution time (directly modifying objects) Cons: * Requires manual manipulation of object properties **Other Considerations** * The benchmark uses a fixed set of test cases and inputs, which may not reflect real-world usage scenarios. * Different browsers or JavaScript engines might have varying performance characteristics for each method. Overall, this benchmark helps identify the most efficient approach for modifying objects in JavaScript, considering factors such as execution speed, memory allocation, and code readability.
Related benchmarks:
Loop perf
Test method Object
For loop vs For...Of loop 3
Test-BC
js spread order
Comments
Confirm delete:
Do you really want to delete benchmark?