function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
var ids = [];
var uuids = [];
var entities = {};
var itemIdByUuid = {};
for(let i = 0; i < 5000; i++) {
const obj = {
id: `id${i}`,
uuid: crypto.randomUUID(),
foo: "bar",
bar: "foo",
};
entities[obj.id] = obj;
ids.push(obj.id);
itemIdByUuid[obj.uuid] = obj.id;
}
const randomUUID = uuids[Math.floor(Math.random() * uuids.length)];
const itemMatchingSomeUUID = Object.values(entities).find((obj) => obj.uuid === randomUUID);
const randomUUID = uuids[Math.floor(Math.random() * uuids.length)];
const itemMatchingSomeUUID = ids.map((id) => entities[id]).find((obj) => obj.uuid === randomUUID);
const randomUUID = uuids[Math.floor(Math.random() * uuids.length)];
const itemMatchingSomeUUID = entities[itemIdByUuid[randomUUID]];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
finding item using find with Object.values | |
finding item using find without Object.values (accessing entities using the ids array) | |
using the itemIdByUuid "projection" we created |
Test name | Executions per second |
---|---|
finding item using find with Object.values | 768.4 Ops/sec |
finding item using find without Object.values (accessing entities using the ids array) | 1698.4 Ops/sec |
using the itemIdByUuid "projection" we created | 2131273.5 Ops/sec |
Let's dive into the Benchmark Definition and individual test cases to understand what is being tested.
Benchmark Definition:
The benchmark measures the performance of three approaches for accessing and finding items in an object by their ID:
find()
method with Object.values()
: This approach uses the find()
method on the array of objects (Object.values(entities)
) to find the item with a matching UUID.entities
object directly using the IDs array (ids
) and then finds the item with a matching UUID using the find()
method.itemIdByUuid
object: This approach uses a precomputed mapping of UUIDs to IDs (itemIdByUuid
) to access the corresponding ID for each UUID and then finds the item in the entities
object.Options compared:
The three approaches are compared to determine which one is the fastest. The results show that:
find()
with Object.values()
has an execution speed of approximately 2131 executions per second.itemIdByUuid
object is the slowest, with an execution speed of about 768 executions per second.Pros and Cons:
Here's a brief summary of the pros and cons of each approach:
find()
with Object.values()
:entities
objectitemIdByUuid
object:itemIdByUuid
)Library:
The benchmark uses the following libraries:
crypto
: used for generating random UUIDsMath.random()
: used for shuffling the array of objectsJavaScript features/syntax:
None.
Other considerations:
find()
method and object lookup.Object.values(entities)
) creates a new array that needs to be managed, which can add overhead.Alternatives:
Other approaches for accessing and finding items in an object by their ID might include:
itemIdByUuid
)However, these alternatives may have trade-offs in terms of memory usage, complexity, and performance.