Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test copy methods (primitive)
(version: 0)
Comparing performance of:
structuredClone vs Direct copy vs JSON vs cloneDeep
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.6.2/lodash.min.js'></script>
Script Preparation code:
var voucher = 5
Tests:
structuredClone
const copy = window.structuredClone(voucher);
Direct copy
const copy = voucher;
JSON
const copy = JSON.parse(JSON.stringify(voucher));
cloneDeep
const copy = _.cloneDeep(voucher)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
structuredClone
Direct copy
JSON
cloneDeep
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
structuredClone
418524.9 Ops/sec
Direct copy
12451991.0 Ops/sec
JSON
1424267.5 Ops/sec
cloneDeep
3696709.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and discussed. **Benchmark Overview** The benchmark measures the performance of different methods to copy a primitive value (in this case, an integer) in JavaScript. The benchmark compares four approaches: 1. `structuredClone` 2. Direct assignment (`const copy = voucher;`) 3. `JSON.parse(JSON.stringify(voucher))` 4. `_.cloneDeep` (using Lodash library) **Approaches and Their Pros/Cons** 1. **Direct Assignment**: This approach is the simplest and most straightforward way to create a copy of the value. However, it may not be suitable for large objects or complex data structures. * Pros: Simple, fast, and easy to implement * Cons: May not preserve object references or properties 2. **JSON.parse(JSON.stringify(voucher))**: This approach uses JSON serialization and deserialization to create a copy of the value. While it's more robust than direct assignment, it can still lead to issues with complex data structures. * Pros: Preserves object references and properties, but may be slower due to serialization overhead * Cons: Can be slower, may not handle circular references well 3. **_.cloneDeep` (using Lodash library)**: This approach uses a dedicated cloning function from the Lodash library to create a deep copy of the value. * Pros: Preserves object references and properties, handles complex data structures, and is often faster than manual serialization * Cons: Requires an external library (Lodash) 4. **structuredClone**: This approach uses a new method introduced in ECMAScript 2022 to create a deep copy of the value. * Pros: Preserves object references and properties, handles complex data structures, and is designed for performance and security * Cons: Requires modern JavaScript engines (ECMAScript 2022+) **Library Overview** The benchmark uses Lodash library (`_.cloneDeep`) to provide a deep cloning function. Lodash is a popular utility library that provides various functions for working with arrays, objects, and other data structures. **Special JS Features/Syntax** This benchmark doesn't use any special JavaScript features or syntax beyond what's required by the `structuredClone` method (introduced in ECMAScript 2022). However, it does rely on modern JavaScript engines that support this new feature. **Other Alternatives** If you don't want to use Lodash, you can implement a deep cloning function using recursive object copying or other techniques. Alternatively, you could use other libraries like Immutable.js or Ramda for deep cloning and manipulation of data structures. However, these alternatives may have different performance characteristics and requirements compared to the benchmark's results. In summary, this benchmark provides an overview of different methods for copying primitive values in JavaScript, highlighting their pros and cons, and showcasing the benefits of modern features like `structuredClone`.
Related benchmarks:
Lodash cloneDeep vs clone vs spread
Clone deep
Object Clone Lodash vs structuredClone
Lodash cloneDeep VS mutate
Lodash lodashoooon 2
Comments
Confirm delete:
Do you really want to delete benchmark?