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]) break;
}
}
for (var i = 0; i < 10; i++) {
const a = obj[`Testing...${i}`]
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array | |
Object |
Test name | Executions per second |
---|---|
Array | 1145941.4 Ops/sec |
Object | 597558.8 Ops/sec |
Let's break down the provided JSON data and explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark definition is a script that sets up two data structures: an array list
and an object obj
. The script then populates both data structures with 10 elements each, using string literals. This creates two separate arrays of strings to be used for searching.
Script Preparation Code
var list = [];
var obj = {};
for (var i = 0; i < 10; i++) {
list[i] = `Testing...${i}`;
obj[`Testing...${i}`] = `Testing...${i}`;
}
This script prepares the data structures and populates them with test data.
Html Preparation Code
There is no HTML preparation code provided, which means that the benchmark does not rely on any specific HTML structure or rendering.
Individual Test Cases
The benchmark has two individual test cases:
for (var i = 0; i < 10; i++) {
const a = `Testing...${i}`;
for (var z = 0; i < 10; i++) {
if (a === list[z]) break;
}
}
This test case searches for an element in the array list
using a nested loop. The outer loop iterates over the elements of the array, and the inner loop checks each element to see if it matches the string literal a
. If a match is found, the loop breaks.
for (var i = 0; i < 10; i++) {
const a = obj[`Testing...${i}`];
}
This test case searches for an element in the object obj
using property access. The string literal a
is used to look up a property on the object, which should return the same value as before.
Library Usage
In both test cases, there are no external libraries being used.
Special JS Features/Syntax
There are no special JavaScript features or syntax being used in these test cases. The code relies solely on standard JavaScript syntax and built-in data structures.
Benchmark Results
The latest benchmark results show the performance of each test case on a Chrome 96 browser on a desktop Windows platform:
Test Case | Executions Per Second |
---|---|
Array | 1145941.375 |
Object | 597558.8125 |
These results indicate that the array search is significantly faster than the object search.
Alternatives
There are several alternatives for benchmarking JavaScript performance, including:
Overall, MeasureThat.net provides a useful platform for testing and comparing the performance of different JavaScript approaches, including array and object searches.