Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
deep json spread vs flat
(version: 0)
Comparing performance of:
spread vs flat
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var deepJson = [ [{ "items": { "item": [ { "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" }, { "id": "1003", "type": "Blueberry" }, { "id": "1004", "type": "Devil's Food" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5007", "type": "Powdered Sugar" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }, ] } }], [{ "items": { "item": [ { "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" }, { "id": "1003", "type": "Blueberry" }, { "id": "1004", "type": "Devil's Food" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5007", "type": "Powdered Sugar" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }, ] } }] ]
Tests:
spread
const a = [...deepJson, ...deepJson, ...deepJson]
flat
const a = [deepJson, deepJson, deepJson].flat()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread
flat
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread
7909334.5 Ops/sec
flat
4332786.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of JavaScript code is crucial in understanding how different approaches can affect execution time. In this benchmark, we have two test cases: `spread` and `flat`. Both tests involve working with nested JSON data. **Benchmark Definition** The benchmark definition for each test case is provided as a string. For the `spread` test, it's: ```javascript const a = [...deepJson, ...deepJson, ...deepJson]; ``` And for the `flat` test, it's: ```javascript const a = [deepJson, deepJson, deepJson].flat(); ``` **Options Compared** In this benchmark, we have two options being compared: using the spread operator (`...`) and using the `flat()` method. The spread operator is used to create a new array by copying elements from an existing array. In this case, it's used to duplicate the `deepJson` array three times. On the other hand, the `flat()` method is used to flatten an array of arrays into a single-level array. Here, it's used to concatenate three instances of the `deepJson` array. **Pros and Cons** **Spread Operator (`...`)** Pros: * More intuitive and readable way to duplicate an array * Works with any type of iterable (not just arrays) Cons: * Can be less efficient than using `flat()` for large datasets, as it creates a new array for each element * Does not preserve the original order of elements **Flat() Method** Pros: * More efficient than spread operator for large datasets, as it only creates one new array * Preserves the original order of elements Cons: * Less intuitive and readable way to duplicate an array * Works with arrays only **Other Considerations** When working with nested JSON data, the performance difference between using the spread operator and `flat()` method may be negligible unless dealing with extremely large datasets. However, if you need to optimize performance for very large JSON data, using `flat()` might be a better choice. In terms of readability and maintainability, the spread operator is generally considered more intuitive and easier to read than `flat()`. This makes it easier to understand the intent of the code and follow its logic. **Latest Benchmark Results** According to the latest benchmark results, Chrome 123 performs slightly better on the `spread` test (with an execution rate of 7909334.5 executions per second) compared to the `flat` test (with an execution rate of 4332786.0 executions per second). However, it's essential to note that these results may vary depending on the specific environment and hardware used. Overall, this benchmark highlights the importance of understanding how different approaches can affect performance in JavaScript code, particularly when working with nested data structures.
Related benchmarks:
RFDC clone vs Lodash cloneDeep vs JSON Parse
RFDC clone (circles: true) vs Lodash cloneDeep vs JSON Clone
Object Cloning Comparsion
klona vs core-js structuredClone vs Lodash cloneDeep vs json parse stringify
Comments
Confirm delete:
Do you really want to delete benchmark?