var testArray = Array.from(Array(1000).keys()).map(i => new Date('2020-01-01 00:00:00.' + i).toString());
var testString = testArray.join(',');
var objectHash = testArray.reduce((acc, key) => {acc[key] = true; return acc;},{});
var testSet = new Set(testArray);
Array.from(Array(1000).keys()).map(i => testArray.includes(new Date('2020-01-01 00:00:00.' + i).toString()));
Array.from(Array(1000).keys()).map(i => testString.includes(new Date('2020-01-01 00:00:00.' + i).toString()));
Array.from(Array(1000).keys()).map(i => !!objectHash[new Date('2020-01-01 00:00:00.' + i).toString()]);
Array.from(Array(1000).keys()).map(i => testSet.has(new Date('2020-01-01 00:00:00.' + i).toString()));
Array.from(Array(1000).keys()).map(i => testArray.indexOf(new Date('2020-01-01 00:00:00.' + i).toString()) > -1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.includes | |
String.includes | |
Object Hash-map | |
ES6 Set | |
Array.indexOf |
Test name | Executions per second |
---|---|
Array.includes | 1072.4 Ops/sec |
String.includes | 1057.2 Ops/sec |
Object Hash-map | 984.0 Ops/sec |
ES6 Set | 981.1 Ops/sec |
Array.indexOf | 1007.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Overview
The provided JSON represents a benchmark that tests the performance of different string matching methods in JavaScript: Array.includes
, String.includes
, Object Hash-map
, ES6 Set
, and Array.indexOf
. The test is designed to measure how quickly each method can find a specific value within an array of 1000 strings.
Options Compared
The benchmark compares the following options:
Array.includes()
: Uses the includes()
method on the testArray
.String.includes()
: Uses the includes()
method on the testString
.Object Hash-map
: Uses a hash map data structure to store the test strings and checks if a value exists in the map.ES6 Set
: Uses an ES6 set data structure to store the test strings and checks if a value exists in the set.Array.indexOf()
: Uses the indexOf()
method on the testArray
.Pros and Cons of Each Approach
Here's a brief summary of the pros and cons of each approach:
Array.includes()
: Pros: Fast, efficient, and widely supported. Cons: May be slower than other methods for very large arrays.String.includes()
: Pros: Fast and efficient for string matching. Cons: Requires creating a new string object, which can lead to performance issues with very large strings.Object Hash-map
: Pros: Can handle large datasets efficiently. Cons: Requires additional memory allocation and may not be as fast as other methods for small datasets.ES6 Set
: Pros: Efficient and concise way to store unique values. Cons: May not be suitable for all use cases (e.g., searching for a specific value).Array.indexOf()
: Pros: Fast and efficient, but may require additional memory allocation. Cons: Requires iterating through the entire array.Library and Purpose
The benchmark uses the following libraries:
Array.from()
: A modern JavaScript method to create an array from an iterable.Set
: An ES6 data structure for storing unique values.Object Hash-map
: A custom implementation of a hash map, which is not a built-in JavaScript data structure.Special JS Features or Syntax
The benchmark does not use any special JavaScript features or syntax, such as async/await, Promises, or ES6 classes.
Alternative Implementations
There are several alternative implementations for each method:
Array.includes()
: indexOf()
, binarySearch()
String.includes()
: indexOf()
, regular expressionsObject Hash-map
: Map
(built-in JavaScript), custom hash map libraries like fast-integer-keys
ES6 Set
: Built-in Set
data structure, alternative implementations like fast-set
Array.indexOf()
: indexOf()
, binarySearch()Keep in mind that the performance differences between these alternatives may be small, and the choice of implementation depends on specific use cases and requirements.
I hope this explanation helps you understand the benchmarking process on MeasureThat.net!