Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find index range in array1
(version: 0)
Comparing performance of:
sliceRange vs getDatesBetweenRange
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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) { let l = 0, r = arr.length, m, lr, rl, res = []; while (l < r) { m = ~~(l + (r - l) / 2); if (arr[m] < min) l = m + 1; else if (arr[m] > min) r = m; else break; } lr = m; rl = m; while (l < lr) { m = ~~(l + (lr - l) / 2); if (arr[m] < min) l = m + 1; else lr = m; } 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]; }
Tests:
sliceRange
sliceRange(rows, 70000, 70400)
getDatesBetweenRange
getDatesBetweenRange(rows, 70000, 70400)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
sliceRange
getDatesBetweenRange
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested on the provided JSON. **Benchmark Definition** The benchmark definition is a script that defines two functions: `sliceRange` and `getDatesBetweenRange`. These functions are designed to operate on an array of numbers (`rows`) generated by another script (not shown in this snippet). * The `sliceRange` function takes an array and a minimum value as input. It returns an array of indices where the elements in the original array exceed the minimum value. * The `getDatesBetweenRange` function is not designed to operate on dates; it appears to be incorrectly named, as there's no date-related functionality in this code snippet. Instead, it seems to be an implementation of a binary search algorithm for finding a range within an array of numbers. **Options Compared** There are two test cases: 1. `sliceRange(rows, 70000, 70400)`: This test case measures the performance of the `sliceRange` function when searching for elements in the `rows` array that fall within a specific range (70000 to 70400). 2. `getDatesBetweenRange(rows, 70000, 70400)`: This test case is not well-suited for its intended purpose and should be considered deprecated or incorrect. **Pros and Cons of Approaches** 1. **Binary Search Algorithm**: The implementation of the binary search algorithm used in `sliceRange` and `getDatesBetweenRange` functions is generally efficient, with a time complexity of O(log n). However, it requires careful handling of edge cases and may not perform well on arrays with limited range or highly unbalanced elements. 2. **Array Indexing**: The array indexing approach (e.g., using `arr[m]`) can be less efficient than the binary search algorithm, especially for large arrays, as it involves more memory accesses and potential bounds checking. **Library and Its Purpose** There is no library explicitly mentioned in this code snippet, but some libraries like Lodash or Ramda might provide similar functionality. If such a library were used, its purpose would be to simplify the implementation of common algorithms and data structures, making the code more concise and readable. **Special JS Features or Syntax** This code snippet does not use any special JavaScript features or syntax, except for possibly using `Math.floor` which is a standard function in JavaScript. However, some minor optimizations like using `~~` to perform integer division could be considered a minor performance optimization but not worth mentioning as special feature. **Other Alternatives** For similar tasks, you might consider the following alternatives: * **Arrays.prototype.findIndex() and Arrays.prototype.findIndexRight()**: These methods are built into the JavaScript Array prototype and can be used to find the index of the first element that satisfies a condition. They provide an efficient way to search for elements in arrays. * **Binary Search Algorithms with Optimizations**: Depending on the specific requirements, you might want to consider optimizing binary search algorithms for performance or using more specialized data structures like heaps or tries. In summary, this benchmark tests the performance of two functions: `sliceRange` and `getDatesBetweenRange`, which are designed to operate on an array of numbers. The implementations use a binary search algorithm and array indexing approach, respectively. While these approaches have their pros and cons, they are generally efficient for searching arrays.
Related benchmarks:
find index range in array
find index range in array11
find index range in array111
find index range in array2
Comments
Confirm delete:
Do you really want to delete benchmark?