function randomIntFromInterval(min, max) {
// min and max included
return Math.floor(Math.random() * (max - min + 1) + min);
}
var rows = [];
let lasth = 0;
for (let i = 0; i < 10000; i++) {
lasth += randomIntFromInterval(16, 50);
rows.push(lasth);
}
function sliceRange(arr, min, max) {
var l = 0,
r = arr.length, res = [];
rough: {
while (l < r) {
var m = ~~(l + (r - l) / 2);
if (arr[m] < min) l = m + 1;
else if (arr[m] > max) r = m;
else break rough;
}
return [];
}
var lr = m,
rl = m;
while (l < lr) {
m = ~~(l + (lr - l) / 2);
if (arr[m] < min) l = m + 1;
else lr = m;
}
while (rl < r) {
m = ~~(rl + (r - rl) / 2);
if (arr[m] > max) r = m;
else rl = m + 1;
}
for (let i = 0; i < 60; i += 1) {
const rid = i + l;
if (rid < arr.length) res.push(rid);
}
return res;
}
function getDatesBetweenRange(dates, min, max) {
var subArray = [];
var value, iCntr;
var start, end;
var low = 0,
high = dates.length - 1;
while (high - low > 1) {
centre = Math.floor((high + low) / 2);
if (dates[centre] < min) low = centre;
else high = centre;
}
start = low;
high = dates.length - 1;
while (high - low > 1) {
centre = Math.floor((high + low) / 2);
if (dates[centre] > max) high = centre;
else low = centre;
}
end = high;
return [start-1,end+1];
}
sliceRange(rows, 70000, 70400)
getDatesBetweenRange(rows, 70000, 70400)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
sliceRange | |
getDatesBetweenRange |
Test name | Executions per second |
---|---|
sliceRange | 1967762.9 Ops/sec |
getDatesBetweenRange | 55486.7 Ops/sec |
Let's dive into the provided JSON data to understand what is being tested.
Benchmark Definition
The benchmark definition represents a JavaScript function that takes an array, and two bounds as input, and returns an array of indices within the original array that fall within the given range. In this case, we have two functions:
sliceRange(arr, min, max)
: This function is designed to return all indices in the array arr
that are greater than or equal to min
and less than or equal to max
.getDatesBetweenRange(dates, min, max)
: This function seems to be related to finding dates within a range, but it's not directly clear what its purpose is without more context.Options Compared
The benchmark compares the execution performance of these two functions across different browsers and devices.
Approaches Compared
There are two main approaches being compared:
sliceRange
and getDatesBetweenRange
use a binary search approach to find indices within a range. However, their implementation details differ.sliceRange
function uses an approximation technique called "rough" (explained later) to estimate the number of elements in the array that fall within the given range.Pros and Cons
Rough Technique
The "rough" technique is an optimization used in the sliceRange
function to quickly estimate the number of elements in the array that fall within the given range. It works by:
min
.max
.This approach is faster but may not always provide accurate results.
Library Used
There doesn't appear to be any external libraries used in this benchmark.
Special JS Features/Syntax
The benchmark uses some JavaScript features and syntax, such as:
Math.floor
to round down a number.Math.random
to generate random numbers.\r\n
) for formatting code.Alternatives
If you were to rewrite this benchmark, you could consider alternative approaches such as:
Array.prototype.indexOf
) to quickly find elements within a range.Keep in mind that these alternatives may require additional libraries or infrastructure, and their implementation would depend on the specific requirements of your use case.