<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
var items = {
a: {
b: 'b',
c: {
d: 'd',
},
},
b: {
b: 'b',
c: {
d: 'd',
},
},
c: {
b: 'b',
c: {
d: 'd',
},
},
d: {
b: 'b',
c: {
d: 'd',
},
},
e: {
b: 'b',
c: {
d: 'd',
},
},
}
var cope = _.clone(items);
var cope = Object.create(items);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.clone | |
object.create |
Test name | Executions per second |
---|---|
_.clone | 1734677.9 Ops/sec |
object.create | 41716576.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares two approaches: object.create
and _
.clone` (using Lodash). Both methods are used to create a deep copy of an object.
What is being tested?
In this benchmark, the test cases create a complex object with multiple levels of nesting (items
). The goal is to measure which approach creates a deeper copy of the original object.
Options compared
There are two options being compared:
_.clone(items)
: This uses Lodash's _
namespace, which provides a clone
function for deep copying objects.Object.create(items)
: This uses the built-in Object.create()
method to create a new object with the specified prototype (in this case, items
).Pros and Cons of each approach
_.clone(items)
:Object.create(items)
:Library: Lodash
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string formatting, and object creation. The _
namespace includes various helper functions like clone
, which is used in this benchmark.
Special JS feature/syntax
There are no special features or syntax being tested in this benchmark. Both approaches use standard JavaScript syntax to create the object and copy it.
Other alternatives
If you're interested in alternative approaches, here are a few more:
JSON.parse(JSON.stringify(items))
: This creates a deep copy of the original object by parsing the JSON representation of the object as a string, which is then parsed back into an object.deepcopy
or obj-freeze
: These libraries provide functions specifically designed for creating deep copies of objects.Keep in mind that while these alternatives might be viable options, they may not be as efficient or straightforward to use as the Object.create()
method or Lodash's _
namespace.