var objList= [
{ name: "Lorem" },
{ name: "ipsum" },
{ name: "dolor" },
{ name: "sit" },
{ name: "amet" },
{ name: "consectetur" },
{ name: "adipisicing" },
{ name: "elit." },
{ name: "Ullam" },
{ name: "nesciunt" },
{ name: "debitis," },
{ name: "facilis" },
{ name: "qui" },
{ name: "possimus" },
{ name: "minus" },
{ name: "rerum" },
{ name: "velit" },
{ name: "saepe" },
{ name: "magnam," },
{ name: "voluptatum" },
{ name: "quos" },
{ name: "eveniet" },
{ name: "ducimus" },
{ name: "facere" },
{ name: "harum" },
{ name: "itaque" },
{ name: "atque" },
{ name: "assumenda" },
{ name: "reiciendis" },
{ name: "exercitationem" }
]
for(let i = 0; i < 100000; i++)
objList.unshift({ name: ""+i })
var object = objList.reduce((acc, team) => {
acc[team.name] = team
return acc;
}, {});
var b = object["consectetur"]
var map = objList.reduce((acc, team) => {
acc.set(team.name, team);
return acc;
}, new Map());
for(let i = 0; i < 100000; i++)
var b = map.has("consectetur")
var set = objList.reduce((acc, team) => {
acc.add(team.name);
return acc;
}, new Set());
for(let i = 0; i < 100000; i++)
var b = set.has("consectetur")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object lookup | |
Map lookup | |
Set lookup |
Test name | Executions per second |
---|---|
Object lookup | 225.1 Ops/sec |
Map lookup | 233.0 Ops/sec |
Set lookup | 274.3 Ops/sec |
The benchmark provided is designed to compare the performance of three different data structures in JavaScript for looking up items based on a name attribute from an array of objects. The three approaches being compared are:
Object Lookup
objList
) into an object where each key is the name of the item, and the corresponding value is the object itself. After constructing this object, it executes a lookup for the item with the name "consectetur".var object = objList.reduce((acc, team) => {
acc[team.name] = team;
return acc;
}, {});
var b = object["consectetur"];
Map
, especially when the keys are simple strings.Map Lookup
objList
into a Map
where names are keys and objects are values. It then checks whether the name "consectetur" exists in the Map
.var map = objList.reduce((acc, team) => {
acc.set(team.name, team);
return acc;
}, new Map());
for(let i = 0; i < 100000; i++)
var b = map.has("consectetur");
Set Lookup
Set
, which is a collection of unique values. Here, the test converts the names of the items in objList
to a Set
and checks if "consectetur" exists in that Set
.var set = objList.reduce((acc, team) => {
acc.add(team.name);
return acc;
}, new Set());
for(let i = 0; i < 100000; i++)
var b = set.has("consectetur");
From the latest benchmark results, the following execution speeds (executions per second) were observed:
The Set
structure performed the best, which is somewhat expected given its design to handle presence checks efficiently. Map
also performed reasonably well but slightly slower than Set
. The object approach was the slowest of the three, potentially due to overhead in property lookups or object creation.
Choice of Data Structure: The decision to use one data structure over another generally depends on the specific requirements of the task at hand. If you need to ensure uniqueness, a Set
is appropriate. If you need more complex key-value associations and optimal performance with non-string keys, a Map
is ideal. For simple lookups and the lowest memory footprint, a plain object may be sufficient.
Browser and Environment: Benchmark results can be impacted by browser optimizations and the specific JavaScript engine in use since different browsers might handle memory and data structure performance differently.
localStorage
or IndexedDB
in a web context, although they come with their own sets of performance and usability considerations.