const myArr =[];
const myMap = new Map();
const myObj = {};
function getFromArr(key){
return myArr.find(n=>n.id===key);
}
function getFromMap(key){
return myMap.get(key);
}
function getFromObj(key){
return myObj[key];
}
// PUPULATE DATA
const size = 25000;
let val, key;
for (let i=0;i<size;i++){
key = `file_${i}`;
val = Math.floor ((Math.random()*100000));
myArr.push({
id: `file_${i}`,
value: val
});
myMap.set(key,val);
myObj[key] = val;
}
const TEST_ITERATIONS = size;
function runPerfOnFunction(funcToPull, iterationCount, testName){
for (let i=0;i<iterationCount;i++){
let id = Math.floor ((Math.random()*size))
key = `file_${id}`;
funcToPull(key);
}
}
runPerfOnFunction(getFromArr, 25000, 'myArr Pull');
runPerfOnFunction(getFromObj, 25000, 'myObj Pull');
runPerfOnFunction(getFromMap, 25000, 'myMap Pull');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array | |
Object | |
Map |
Test name | Executions per second |
---|---|
Array | 0.7 Ops/sec |
Object | 125.4 Ops/sec |
Map | 130.3 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition
The provided JSON represents a benchmark definition for three different data structures: an array, an object, and a map. The script preparation code defines three functions:
getFromArr(key)
: Retrieves an element from the array by its index (id
).getFromMap(key)
: Retrieves a value from the map by its key.getFromObj(key)
: Retrieves a property from the object by its key.The script also populates these data structures with 25,000 elements and defines a function runPerfOnFunction(funcToPull, iterationCount, testName)
that runs each of these functions for a specified number of iterations.
Options Compared
In this benchmark, two options are compared:
getFromArr
function is used to retrieve elements from the array.getFromObj
function is used to retrieve properties from the object.getFromMap
function is used to retrieve values from the map.Pros and Cons of Each Approach
Here's a brief analysis of each approach:
in
operator or bracket notation ([key]
), which is optimized by modern JavaScript engines..get()
method, which is optimized by modern JavaScript engines.get()
will return undefined
.Library and Purpose
None of the libraries used in this benchmark are explicitly mentioned in the provided JSON. However, we can infer that the Map
object is a built-in JavaScript feature since its implementation has improved over time.
Special JS Features or Syntax
There's no explicit use of special JavaScript features or syntax in this benchmark. The code uses basic functions and data structures.
Alternatives
If you want to optimize your own data structures or search algorithms, consider the following alternatives:
Keep in mind that each alternative has its own trade-offs and requirements for implementation and usage.
In conclusion, the MeasureThat.net benchmark provides a simple yet informative comparison of three data structures: array, object, and map. By understanding the pros and cons of each approach, developers can make more informed decisions about choosing the right data structure or search algorithm for their specific use case.