Permalink
Browse files

[bisection] performance optimization

  • Loading branch information...
1 parent d0057e1 commit 3923a4d61db62bb49fb15b6311a22376a458c8de @indutny indutny committed Apr 22, 2011
Showing with 4 additions and 4 deletions.
  1. +4 −4 lib/bisection.js
View
@@ -10,15 +10,15 @@
exports.Bisection = function bisection(array, x, low, high){
// The low and high bounds the inital slice of the array that needs to be searched
// this is optional
- low = low || 0;
- high = high || array.length;
+ low || (low = 0);
+ high || (high = array.length);
var mid;
if (low < 0) throw new Error('Low must be a non-negative integer');
while(low < high){
- mid = Math.floor((low + high) / 2);
+ mid = (low + high) >> 1;
if (x < array[mid]){
high = mid;
@@ -28,4 +28,4 @@ exports.Bisection = function bisection(array, x, low, high){
}
return low;
-};
+};

0 comments on commit 3923a4d

Please sign in to comment.