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;
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;
}
return [l-1, r+1];
}
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 | 3637731.0 Ops/sec |
getDatesBetweenRange | 54763.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark measures the performance of two functions: sliceRange
and getDatesBetweenRange
. The test data is an array rows
created by generating random integers between 16 and 50, with a total length of 10,000 elements. Each function takes three arguments:
arr
: the input array (rows
)min
and max
: the range boundaries (lower and upper bounds)sliceRange Function
The sliceRange
function is designed to find the indices of a given range within the input array. It uses a binary search approach to find the first element that exceeds or equals the minimum value (l
) and the last element that is less than or equal to the maximum value (r
). The function returns an array with two elements: [l-1, r+1]
, representing the indices of the range.
getDatesBetweenRange Function
The getDatesBetweenRange
function appears to be designed for date-based ranges. However, upon closer inspection, it seems that the input dates
is actually the same as the array rows
. The function is supposed to find the dates (or in this case, random integers) between a given range (min
and max
). It uses a similar binary search approach to find the first date that exceeds or equals the minimum value (start
) and the last date that is less than or equal to the maximum value (end
). The function returns an array with two elements: [start-1, end+1]
.
Comparison of Approaches
The sliceRange
and getDatesBetweenRange
functions use similar binary search approaches. However, there are some differences:
sliceRange
uses a more straightforward approach, finding the first element that exceeds or equals the minimum value and the last element that is less than or equal to the maximum value.getDatesBetweenRange
also finds the first and last elements of the range, but it seems to be using the same array as input.Pros and Cons
The pros of these approaches include:
However, there are some potential cons:
sliceRange
may not work correctly if the input array is sorted in ascending order (instead of descending), as it finds the first element that exceeds or equals the minimum value and then takes the last index that meets this condition. This could lead to incorrect results.getDatesBetweenRange
seems to be using a similar approach to sliceRange
, but with some unclear assumptions about the input data.Other Considerations
Another consideration is the potential impact of the input data on the performance of these functions. For example:
rows
) may also impact the performance, as larger arrays can lead to more cache misses and slower search times.Library Usage
Neither sliceRange
nor getDatesBetweenRange
seems to be using any external libraries. However, if we assume that dates
is supposed to be a separate array of dates (which it's not), then the implementation might require additional library support for date-based comparisons.
Special JS Features or Syntax
There are no special JavaScript features or syntax used in these implementations. The code appears to be using standard JavaScript and DOM APIs.