var list = [];
var obj = {};
for (var i = 0; i < 10; i++) {
list[i] = `Testing...${i}`
obj[`Testing...${i}`] = `Testing...${i}`
}
for (var i = 0; i < 10; i++) {
const a = `Testing...${i}`
for (var z = 0; i < 10; i++) {
if (a === list[z]) {
const b = list[z];
break;
}
}
}
for (var i = 0; i < 10; i++) {
const a = `Testing...${i}`
for (var i = 0; i < 10; i++) {
const b = obj[a]
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array | |
Object |
Test name | Executions per second |
---|---|
Array | 872690.1 Ops/sec |
Object | 1069669.9 Ops/sec |
Let's break down the provided benchmark.
Benchmark Definition
The benchmark is designed to compare the performance of searching for an element in two different data structures: arrays and objects.
Options being compared
Two options are being compared:
Pros and Cons
Library and purpose
None of the test cases use a specific library, but they do rely on built-in JavaScript features:
Special JS feature or syntax
The break
statement is used in both test cases to exit the loop when the target element is found. This is a built-in JavaScript feature that allows for early termination of loops.
Other alternatives
To improve performance, other approaches could be considered:
Array.prototype.includes()
to find an element in an array.Object.keys().forEach()
to iterate over both the keys and values of an object.These alternatives might offer better performance profiles, but they also introduce additional overhead or dependencies on modern JavaScript features.