<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
<script src="https://cdn.rawgit.com/ivolovikov/fastest-clone/master/index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.4/benchmark.min.js"></script>
// intial data
var source = {a:1,b:1,c:1,d:1,e:1,f:1,g:1,h:{a:-1,b:1,c:1,d:1,e:1,f:1,g:1}};
var result = _.cloneDeep(source);
// adding checking to prevent compiler optimization
if (result.h.a != -1) throw new Error('Object not clonned');
result.h.a = Math.random();
var result = R.clone(source);
// adding checking to prevent compiler optimization
if (result.h.a != -1) throw new Error('Object not clonned');
result.h.a = Math.random();
var CloneFactory = FastClone.factory(source);
var result = new CloneFactory(source);
// adding checking to prevent compiler optimization
if (result.h.a != -1) throw new Error('Object not clonned');
result.h.a = Math.random();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Ramda without relying on currying or composition | |
Fast Clone |
Test name | Executions per second |
---|---|
Lodash | 191458.0 Ops/sec |
Ramda without relying on currying or composition | 334130.5 Ops/sec |
Fast Clone | 53560.3 Ops/sec |
Measuring performance differences between various JavaScript libraries and implementations is crucial in the development world.
Overview of Tested Options
The benchmark compares three options for creating a deep clone of an object:
cloneDeep
function to create a deep copy of an object.clone
function to create a deep clone of an object, but with some variations in its usage and behavior compared to the standard implementation.Comparison of Options
Lodash's cloneDeep
function is generally reliable and efficient. However, it has two main drawbacks:
To mitigate these risks, Lodash provides a way to prevent compiler optimization by introducing unnecessary checks. However, this comes at the cost of slightly increased execution time.
Ramda's clone
function is similar to Lodash's but has some differences in its usage and behavior:
Ramda's clone function also includes some checks to prevent compiler optimization, which can impact execution time.
Fastest Clone is an optimized implementation of a deep clone function that bypasses the common pitfalls associated with libraries like Lodash:
However, this optimized version might not be as familiar or well-tested as the more established libraries like Lodash.
Considerations
When choosing between these options, consider the following factors:
Alternative Options
Other alternatives to these libraries include:
clone
-focused counterpart.