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++) {
if (shuffled[i].position) {}
}
for (let i=0; i<200000; i++) {
if (sorted[i].position) {}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Shuffled | |
Sorted |
Test name | Executions per second |
---|---|
Shuffled | 15.7 Ops/sec |
Sorted | 31.3 Ops/sec |
Let's dive into the explanation.
Benchmark Definition JSON
The provided JSON represents a JavaScript microbenchmark named "Shuffled Array Cache Test 2". It consists of two benchmark definitions:
sorted
and shuffled
, each with 200,000 elements, and a class Vector
used to represent object properties. The script then populates these arrays with objects and shuffles the second array.Individual Test Cases
The JSON also includes two individual test cases:
if
statement.if
statement.Options Compared
The benchmark compares two approaches:
if
statement (shuffled[i].position
).if
statement (sorted[i].position
).Pros and Cons
Library and Purpose
In the script preparation code, a class Vector
is defined. This is likely used as a placeholder or a representation of an object property, but its actual purpose depends on the context of the benchmark. The library used here seems to be a custom implementation, rather than a widely recognized one.
Special JS Feature or Syntax
There are no special JavaScript features or syntax used in this benchmark. It only relies on standard ECMAScript features.
Alternatives
Other alternatives for similar benchmarks might include:
Keep in mind that these alternatives might not be directly applicable to the existing benchmark, and new benchmarks should consider their own unique use cases and requirements.