<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
function generateRandomStrings(count = 1000, length = 32) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charLen = chars.length;
return Array.from({ length: count }, () => {
let s = '';
for (let i = 0; i < length; i++) {
s += chars.charAt(Math.floor(Math.random() * charLen));
}
return s;
});
}
function arraysEqualBySorting(a, b) {
if (a.length !== b.length) return false;
return _.isEqual(_.sortBy(a), _.sortBy(b));
}
var first = generateRandomStrings();
var second = generateRandomStrings();
arraysEqualBySorting(generateRandomStrings(10000, 32), generateRandomStrings(10000, 32))
arraysEqualBySorting(generateRandomStrings(100000, 32), generateRandomStrings(100000, 32))
var a = generateRandomStrings(100000, 32)
arraysEqualBySorting(a, a)
arraysEqualBySorting(generateRandomStrings(100000, 128), generateRandomStrings(100000, 128))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
With 10000 | |
With 100000 | |
With 100000 and same arrays | |
With 100000 and 128 strings |
Test name | Executions per second |
---|---|
With 10000 | 26.9 Ops/sec |
With 100000 | 2.3 Ops/sec |
With 100000 and same arrays | 3.6 Ops/sec |
With 100000 and 128 strings | 0.5 Ops/sec |
The benchmark titled "Checking array equality" is designed to evaluate the performance of two functions that check whether two arrays of random strings are equal or not. The tests focus on comparing different array sizes and configurations to understand how the implementation performs under varying conditions.
Random String Generation:
The benchmark begins by generating arrays of random strings using the generateRandomStrings
function. This function creates an array of a specified length, generating strings of a specified length composed of alphanumeric characters.
count
: Number of strings to generate.length
: Length of each string._.isEqual
for deep comparison, and _.sortBy
for sorting the arrays.Array Comparison:
The method arraysEqualBySorting
checks whether two arrays are equal by:
_.isEqual
to determine equality after sorting.The benchmark runs the following scenarios:
With 10000:
Compares two arrays of 10,000 random strings of 32 characters each.
With 100000 and same arrays:
Compares two identical arrays of 100,000 random strings, both 32 characters long.
With 100000:
Similar to the first test, but working with 100,000 random strings to evaluate the function's performance on larger datasets.
With 100000 and 128 strings:
This tests the performance of comparing two arrays of 100,000 strings where each string is 128 characters long.
The benchmark results yield ExecutionsPerSecond
metrics for each case, presenting a clear picture of how performance scales based on data size and complexity:
Other potential approaches to comparing array equality include:
In conclusion, this benchmark serves as an effective tool for understanding some common performance pitfalls in array comparisons in JavaScript, offering insights into how algorithmic complexity can affect runtime efficiency.