var sorted = new Array(200000);
var shuffled = new Array(200000);
class Vector {
constructor() {
this.x = 0;
this.y = 0;
this.z = 0;
}
}
for (let i=0; i<200000; i++) {
const obj = { position: new Vector(), scale: new Vector(), rotation: new Vector() };
sorted[i] = obj;
shuffled[i] = obj;
}
for (let i=0; i<200000; i++) {
const index = Math.floor(Math.random() * 200000);
const temp = shuffled[i];
shuffled[i] = shuffled[index];
shuffled[index] = temp;
}
for (let i=0; i<200000; i++) {
const obj = sorted[i];
if (obj.position.x === 0) {}
}
for (let i=0; i<200000; i++) {
const obj = shuffled[i];
if (obj.position.x === 0) {}
}
const reordered = new Array(200000);
for (let i=0; i<200000; i++) {
reordered[i] = shuffled[i];
}
for (let i=0; i<200000; i++) {
const obj = reordered[i];
if (obj.position.x === 0) {}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Sorted | |
Shuffled | |
Shuffled reorder |
Test name | Executions per second |
---|---|
Sorted | 31.2 Ops/sec |
Shuffled | 14.8 Ops/sec |
Shuffled reorder | 26.1 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared options, pros/cons of each approach, and other considerations.
Benchmark Overview
The benchmark measures the performance of accessing elements in an array by their index. There are three test cases:
Benchmark Definition JSON
The benchmark definition contains two scripts: Script Preparation Code
and Html Preparation Code
. The former creates an array of 200,000 objects with properties position
, scale
, and rotation
. The latter is empty.
Individual Test Cases
Each test case has a unique script that implements the access pattern. Let's analyze each one:
Comparison of Access Patterns
The three access patterns differ in how they handle out-of-bounds or invalid indices:
Pros and Cons of Each Approach
Library and Special JS Features
The benchmark uses a custom Vector
class to represent 3D positions, scales, and rotations. This library is not part of the standard JavaScript API and is specific to this benchmark.
Other Considerations
Alternatives
If you want to run this benchmark on a different platform or browser, you can:
Script Preparation Code
to use a different data structure or algorithm.Html Preparation Code
to change the browser or device platform.Keep in mind that the benchmark's performance results may vary depending on the specific hardware and software configuration.