<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
window.foo = ['cat', 'dog', 'bird'];
window.bar = ['cat', 'dog', 'bird'];
_.isEqual(window.foo, window.bar)
window.foo.toString() === window.bar.toString();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.isEqual | |
JSON.stringify |
Test name | Executions per second |
---|---|
_.isEqual | 1045528.2 Ops/sec |
JSON.stringify | 1194432.4 Ops/sec |
Let's break down what's being tested on the provided JSON.
Benchmark Definition
The benchmark is testing two individual test cases:
_.isEqual(window.foo, window.bar)
:isEqual
function.foo
and bar
variables are arrays created in the script preparation code, and they contain the same elements (['cat', 'dog', 'bird']
).window.foo.toString() === window.bar.toString()
:toString()
method.foo
and bar
variables are arrays created in the script preparation code, and they contain the same elements (['cat', 'dog', 'bird']
). However, the test uses toString()
to convert these arrays to strings, and then compares them using the ===
operator.Options Compared
The benchmark is comparing two approaches:
_.isEqual(window.foo, window.bar)
):toString()
(window.foo.toString() === window.bar.toString()
):Library: Lodash
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, and more. In this benchmark, the isEqual
function is used to compare two arrays for equality.
Special JS Feature/Syntax
There isn't any special JavaScript feature or syntax being tested in these benchmarks. The test cases rely on basic JavaScript concepts, such as array creation and comparison using the ===
operator.
Other Alternatives
If you want to try similar benchmarks, here are some alternatives:
toString()
, you could use JSON.stringify()
or Array.prototype.join()
to convert arrays to strings and compare them.Keep in mind that the choice of approach depends on your specific use case and requirements. The Lodash isEqual
function provides a convenient and optimized way to compare arrays, but it may not be the best choice for all scenarios.