var arr = [];
for (var x = 0; x < 1000; x++)
{
arr[x]=[];
}
for (var i=0; i < 1000; i++)
{
for (var j=0; j < 1000; j++)
{
arr[i][j]=i*j;
}
}
for (var i=0; i < 1000; i++)
{
for (var j=0; j < 1000; j++)
{
arr[j][i]=i*j;
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
IJ | |
JI |
Test name | Executions per second |
---|---|
IJ | 10.0 Ops/sec |
JI | 9.3 Ops/sec |
Let's break down the JavaScript microbenchmark provided by MeasureThat.net.
Benchmark Definition
The benchmark is defined in two separate JSON objects:
Script Preparation Code
: This code initializes an empty array arr
with 1000 elements, each of which is also an empty array.Html Preparation Code
: There is no HTML preparation code specified.In essence, the benchmark prepares a 2D array arr
with dimensions 1000x1000 and then populates it with values obtained by multiplying two indices (i
and j
) together.
Test Cases
There are two test cases:
IJ
: This test case iterates over i
from 0 to 999 and for each iteration, it also iterates over j
from 0 to 999. The result is stored in the array at position (i, j)
.JI
: This test case is similar to IJ
, but with the loop order reversed (iterating over j
before i
). Again, the result is stored in the array at position (i, j)
.Library
There are no external libraries mentioned in this benchmark definition.
Special JS Features/Syntax
Neither of the two test cases uses any special JavaScript features or syntax that is not commonly available. They use basic variables, loops, and arithmetic operations.
Options Compared
The two test cases differ only in the order of their loops:
IJ
: Iterates over i
first and then over j
.JI
: Iterates over j
first and then over i
.This difference will likely result in different performance characteristics, as it may affect cache locality or other memory-related optimizations.
Pros and Cons of Different Approaches
The choice between IJ
and JI
depends on the specific requirements and constraints of your application. Here are some pros and cons of each approach:
IJ (Iterate over i
first)
Pros:
i
and j
) tend to be stored in adjacent memory locations.Cons:
JI (Iterate over j
first)
Pros:
Cons:
i
and j
) may not be stored in adjacent memory locations, leading to reduced cache locality.Other Alternatives
If you're interested in exploring alternative approaches or test cases, consider the following options:
IJ
and JI
, so that one iterates over i
first and the other iterates over j
first.Keep in mind that these alternatives will likely have varying degrees of impact on performance, and it's essential to consider your specific use case and requirements before modifying the benchmark.