Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
binary sorting
(version: 0)
Comparing performance of:
while loop vs function recursive
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const arr = [...Array(100).keys()]; const x = 24; const y = 101; let start=0, end=arr.length-1; let found = false; let mid = 0;
Tests:
while loop
const arr = [...Array(100).keys()]; const x = 24; const y = 101; let start=0, end=arr.length-1; let found = false; let mid = 0; // Iterate while start not meets end while (start<=end && found === false){ // Find the mid index mid=Math.floor((start + end)/2); if (arr[mid] === x) { found = true; // Else look in left or right half accordingly } else if (arr[mid] < x) { start = mid + 1; } else { end = mid - 1; } }
function recursive
const arr = [...Array(100).keys()]; const x = 24; const y = 101; let start=0, end=arr.length-1; let found = false; let mid = 0; function bsearch(start, end) { if (start > end) return false; mid=Math.floor((start + end)/2); if (arr[mid] === x) return true; if(arr[mid] > x) return bsearch(start, mid-1); else return bsearch(mid+1, end); } found = bsearch(start, end);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
while loop
function recursive
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):
**Benchmark Overview** The provided JSON represents a JavaScript benchmark for binary searching in an array. The test case is designed to compare the performance of two approaches: using a while loop and using a function with recursion. **Options Compared** The two options being compared are: 1. **While Loop**: This approach uses a traditional while loop to find the midpoint of the search range and then recursively searches for the target element in either the left or right half. 2. **Function Recursive**: This approach defines a recursive function `bsearch` that takes the start and end indices as arguments. It calculates the midpoint, compares it with the target element, and if not found, calls itself with the adjusted search range. **Pros and Cons of Each Approach** * **While Loop**: + Pros: More efficient in terms of memory usage since it doesn't require function call overhead. + Cons: Can be less readable and maintainable due to its imperative nature. * **Function Recursive**: + Pros: More concise and easier to understand, as the recursive logic is encapsulated within a single function. + Cons: May incur higher memory costs due to stack frame allocation for each recursive call. **Library Used** The `bsearch` function in the recursive approach uses the `Array.prototype.includes()` method, which is a built-in JavaScript library function. Its purpose is to find an element in an array using a binary search algorithm. **Special JS Feature or Syntax** This benchmark does not explicitly use any special JavaScript features or syntax beyond the standard ECMAScript features. However, it relies on the `Array.prototype.includes()` method, which may be considered a non-standard feature depending on how one defines "standard." **Other Alternatives** For binary searching in an array, other approaches can include: * Using the `Math.min` and `Math.max` functions to calculate the midpoint. * Implementing a iterative algorithm using bitwise operations (e.g., shifting and masking) to find the midpoint. * Utilizing specialized libraries or data structures optimized for binary search, such as balanced binary search trees. These alternatives might offer different trade-offs in terms of performance, memory usage, and code readability.
Related benchmarks:
javascript count sort vs native sort
Javascript Sorting Algorithms mdhe
Javascript Sorting Algorithms FRAC
Javascript Sorting Algorithms FRACa
Sorting of array
Comments
Confirm delete:
Do you really want to delete benchmark?