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" }
]
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")
for(let i = 0; i < 100000; i++)
var b = objList.some(o=>o.name == "consectetur")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object lookup | |
Map lookup | |
Set lookup | |
Obj array lookup |
Test name | Executions per second |
---|---|
Object lookup | 1635299.2 Ops/sec |
Map lookup | 36647.3 Ops/sec |
Set lookup | 36601.9 Ops/sec |
Obj array lookup | 1750.7 Ops/sec |
The benchmark presented in the JSON focuses on various methods of looking up a value within a structured list of objects. Specifically, it compares the performance of different JavaScript data structures and lookup techniques using a consistently defined object list containing 10,000 items.
Object Lookup:
Array.prototype.reduce
, mapping each object's name
property to the object itself for efficient lookup. The test retrieves an object using the name
key.Map Lookup:
Map
object, which is designed for key-value pairs, to store the objects using their name
property.Set Lookup:
Set
is created to store unique name
values. The test checks for existence of a specific name
in the set.has()
method for checking if an item exists.Object Array Lookup:
Array.prototype.some
to iterate over the array checking if any object's name
matches the search term.From the benchmark results:
When choosing a data structure for lookups, the choice often depends on the requirements for performance, memory usage, and whether the order of items matters (as with Map
). Using objects remains the most straightforward and efficient choice for many scenarios, especially when keys are simple strings.
Other alternatives could include:
Map
and Set
, but with memory management benefits for garbage collection when keys or values are no longer referenced.Overall, each method has strengths and weaknesses, and the optimal choice depends on specific use cases and performance requirements. For an application needing high-frequency lookups, opting for Map
or traditional objects is advisable, while arrays might be more suitable for smaller or infrequency-use cases.