<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
window.foo = ['cat', 'dog', 'bird'];
window.bar = ['cat', 'dog', 'bird'];
_.isEqual(window.foo, window.bar)
JSON.stringify(window.foo) === JSON.stringify(window.bar);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.isEqual | |
JSON.stringify |
Test name | Executions per second |
---|---|
_.isEqual | 1491012.6 Ops/sec |
JSON.stringify | 802116.0 Ops/sec |
Let's break down the provided JSON data and explain what's being tested, compared, and discussed.
Benchmark Overview
The benchmark is designed to compare the performance of two approaches: using Lodash's isEqual
function and comparing strings with ===
operator (i.e., JSON.stringify
). The goal is to determine which approach is faster for equality checks between arrays.
Script Preparation Code
The script preparation code sets up a JavaScript environment where two variables, window.foo
and window.bar
, are assigned the same array value. This setup ensures that both tests compare identical arrays.
Html Preparation Code
The HTML preparation code includes a script tag that loads the Lodash library version 4.17.15 from a CDN. This library provides the isEqual
function used in one of the test cases.
Individual Test Cases
There are two test cases:
isEqual
function to compare the two arrays.JSON.stringify
on both sides of an array.Library and Purpose
Lodash is a utility library that provides various helper functions for tasks like equality checks, string manipulation, and more. In this benchmark, Lodash's isEqual
function is used to compare two arrays. The purpose here is to demonstrate the performance difference between using a specialized equality checking function versus a more general-purpose comparison operator.
Other Considerations
Both test cases are designed to measure execution speed. However, there might be additional factors to consider:
JSON.stringify
cached or recomputed on every iteration?Pros and Cons of Different Approaches
isEqual
function:Browser and Device Specifics
The benchmark result shows that Chrome 81 on Windows (Desktop) was used as the test environment. The reported ExecutionsPerSecond
value suggests that the execution speed of the tests is relatively fast in this environment.
Alternatives
Other approaches for equality checks might include:
every()
, some()
, or includes()
to create a boolean result.These alternatives might have their own set of trade-offs in terms of performance, readability, and maintainability.