window.myObj = {};
window.myMap = new Map();
window.generateId = () => new Date().toISOString() + '=' + Math.random();
window.generateObj = () => ({
'a': generateId(),
'b': generateId(),
'c': generateId()
});
myMap.set(generateId(), generateObj());
myObj[generateId()] = generateObj();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Write to map | |
Write to obj |
Test name | Executions per second |
---|---|
Write to map | 113712.2 Ops/sec |
Write to obj | 110391.1 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
What is being tested?
The provided JSON represents a benchmark test case, which compares two approaches for writing data to objects in JavaScript: using a Map
(a built-in JavaScript object) versus using an ordinary object (Object
). The test measures the performance difference between these two approaches when writing a string key and a value to each data structure.
Options being compared
There are only two options being compared:
Map
object, which is a built-in JavaScript object that stores key-value pairs in a way that allows for efficient lookups, insertions, and deletions.Object
) to store key-value pairs.Pros and Cons
forEach
, entries
, and keys
methods.Other considerations
generateObj()
) for each write operation. Adding more data would impact the performance difference between Maps and ordinary objects.Library usage
There is no explicit library usage mentioned in the benchmark definition or test cases. However, JavaScript engines like V8 (used by Chrome) and SpiderMonkey (used by Firefox) provide built-in support for Maps and other data structures.
Special JS feature/syntax
There are a few features used in this benchmark:
generateId
and generateObj
functions use arrow functions, which are a shorthand way of defining small, one-time-use functions.generateId
function uses string interpolation (+='= '+
) to concatenate strings with other values.Other alternatives
If you want to compare the performance of different data structures or approaches, consider using alternative options like:
Set
instead of an object for fast membership testing.