├── .gitignore ├── .tern-project ├── test └── binarySearch.js ├── README.md ├── gulpfile.js ├── src └── binarySearch.js ├── package.json ├── benchmark ├── binarySearch.js └── sample.js └── UNLICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.tern-project: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": { 3 | "node": {} 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/binarySearch.js: -------------------------------------------------------------------------------- 1 | require('mocha-testcheck').install(); 2 | var assert = require('assert'); 3 | var binarySearch = require('..'); 4 | 5 | describe('binarySearch', function () { 6 | check.it('can find a number in some sorted numbers', [gen.array(gen.int), gen.int], function (list, item) { 7 | list.sort(function (a, b) { 8 | return a - b; 9 | }); 10 | 11 | var base = list.indexOf(item); 12 | var result = binarySearch(list, item); 13 | assert(list[base] === list[result]); 14 | }); 15 | }); 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # binary-search 2 | 3 | This is a binary search implementation in JavaScript that was born from a couple of my blog posts. It focusses on heavy generative testing and benchmarks to make sure it works and runs fast. 4 | 5 | ## License 6 | 7 | This repository is released under [the unlicense][], feel free to do whatever you want. You can find the full license within the `UNLICENSE` file. 8 | 9 | >This is free and unencumbered software released into the public domain. 10 | > 11 | >Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. 12 | 13 | [the unlicense]: http://unlicense.org/ 14 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'); 2 | var mocha = require('gulp-mocha'); 3 | var benchmark = require('gulp-bench'); 4 | 5 | gulp.task('benchmark', function () { 6 | return gulp.src('benchmark/binarySearch.js', { read: false }) 7 | .pipe(benchmark()); 8 | }); 9 | 10 | gulp.task('test', function () { 11 | return gulp.src('test/binarySearch.js', { read: false }) 12 | .pipe(mocha()) 13 | .on('error', keepCalmAndCarryOn); 14 | 15 | function keepCalmAndCarryOn(err) { 16 | console.error(err); 17 | this.emit('end'); 18 | } 19 | }); 20 | 21 | gulp.task('test-watch', ['test'], function () { 22 | return gulp.watch(['src/binarySearch.js', 'test/binarySearch.js'], ['test']); 23 | }); 24 | 25 | gulp.task('default', ['test']); 26 | -------------------------------------------------------------------------------- /src/binarySearch.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Performs a binary search on the provided sorted list and returns the index of the item if found. If it can't be found it'll return -1. 3 | * 4 | * @param {*[]} list Items to search through. 5 | * @param {*} item The item to look for. 6 | * @return {Number} The index of the item if found, -1 if not. 7 | */ 8 | function binarySearch(list, item) { 9 | var min = 0; 10 | var max = list.length - 1; 11 | var guess; 12 | 13 | while (min <= max) { 14 | guess = Math.floor((min + max) / 2); 15 | 16 | if (list[guess] === item) { 17 | return guess; 18 | } 19 | else { 20 | if (list[guess] < item) { 21 | min = guess + 1; 22 | } 23 | else { 24 | max = guess - 1; 25 | } 26 | } 27 | } 28 | 29 | return -1; 30 | } 31 | 32 | module.exports = binarySearch; 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "binary-search", 3 | "version": "1.0.0", 4 | "description": "This is a binary search implementation in JavaScript that was born from a couple of my blog posts. It focusses on heavy generative testing and benchmarks to make sure it works and runs fast.", 5 | "main": "src/binarySearch.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "gulp test" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/Wolfy87/binary-search" 15 | }, 16 | "keywords": [ 17 | "binary", 18 | "search" 19 | ], 20 | "author": "Oliver Caldwell", 21 | "license": "Unlicense", 22 | "bugs": { 23 | "url": "https://github.com/Wolfy87/binary-search/issues" 24 | }, 25 | "homepage": "https://github.com/Wolfy87/binary-search", 26 | "devDependencies": { 27 | "gulp": "^3.8.10", 28 | "gulp-bench": "^1.1.0", 29 | "gulp-mocha": "^2.0.0", 30 | "mocha-testcheck": "^0.1.2" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /benchmark/binarySearch.js: -------------------------------------------------------------------------------- 1 | var binarySearch = require('..'); 2 | var sample = require('./sample'); 3 | 4 | module.exports = { 5 | name: 'binarySearch on 1000 items ranging from -100 to 100', 6 | tests: [ 7 | { 8 | name: 'First', 9 | fn: function () { 10 | binarySearch(sample, -100); 11 | } 12 | }, 13 | { 14 | name: 'Last', 15 | fn: function () { 16 | binarySearch(sample, 100); 17 | } 18 | }, 19 | { 20 | name: '~25%', 21 | fn: function () { 22 | binarySearch(sample, -51); 23 | } 24 | }, 25 | { 26 | name: '~50%', 27 | fn: function () { 28 | binarySearch(sample, 3); 29 | } 30 | }, 31 | { 32 | name: '~75%', 33 | fn: function () { 34 | binarySearch(sample, 52); 35 | } 36 | } 37 | ] 38 | }; 39 | -------------------------------------------------------------------------------- /UNLICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /benchmark/sample.js: -------------------------------------------------------------------------------- 1 | module.exports = [ 2 | -100, 3 | -100, 4 | -100, 5 | -100, 6 | -100, 7 | -99, 8 | -99, 9 | -99, 10 | -99, 11 | -99, 12 | -99, 13 | -99, 14 | -98, 15 | -98, 16 | -98, 17 | -98, 18 | -97, 19 | -97, 20 | -97, 21 | -97, 22 | -97, 23 | -96, 24 | -96, 25 | -96, 26 | -95, 27 | -95, 28 | -95, 29 | -95, 30 | -94, 31 | -94, 32 | -93, 33 | -93, 34 | -93, 35 | -92, 36 | -92, 37 | -92, 38 | -92, 39 | -92, 40 | -92, 41 | -92, 42 | -91, 43 | -91, 44 | -91, 45 | -91, 46 | -91, 47 | -90, 48 | -90, 49 | -89, 50 | -89, 51 | -88, 52 | -88, 53 | -88, 54 | -88, 55 | -88, 56 | -88, 57 | -88, 58 | -88, 59 | -87, 60 | -87, 61 | -87, 62 | -87, 63 | -86, 64 | -86, 65 | -86, 66 | -86, 67 | -86, 68 | -85, 69 | -85, 70 | -85, 71 | -85, 72 | -84, 73 | -84, 74 | -84, 75 | -84, 76 | -84, 77 | -83, 78 | -83, 79 | -83, 80 | -83, 81 | -82, 82 | -82, 83 | -82, 84 | -82, 85 | -81, 86 | -81, 87 | -81, 88 | -81, 89 | -81, 90 | -81, 91 | -80, 92 | -80, 93 | -80, 94 | -80, 95 | -79, 96 | -79, 97 | -79, 98 | -79, 99 | -79, 100 | -79, 101 | -79, 102 | -79, 103 | -79, 104 | -78, 105 | -78, 106 | -78, 107 | -78, 108 | -78, 109 | -78, 110 | -77, 111 | -77, 112 | -77, 113 | -77, 114 | -76, 115 | -76, 116 | -76, 117 | -75, 118 | -75, 119 | -75, 120 | -75, 121 | -75, 122 | -75, 123 | -75, 124 | -75, 125 | -75, 126 | -74, 127 | -74, 128 | -74, 129 | -74, 130 | -73, 131 | -73, 132 | -73, 133 | -73, 134 | -72, 135 | -72, 136 | -72, 137 | -72, 138 | -72, 139 | -72, 140 | -71, 141 | -71, 142 | -71, 143 | -71, 144 | -71, 145 | -71, 146 | -70, 147 | -70, 148 | -70, 149 | -70, 150 | -70, 151 | -70, 152 | -69, 153 | -69, 154 | -69, 155 | -69, 156 | -69, 157 | -68, 158 | -68, 159 | -68, 160 | -68, 161 | -68, 162 | -67, 163 | -67, 164 | -67, 165 | -67, 166 | -66, 167 | -66, 168 | -66, 169 | -65, 170 | -65, 171 | -65, 172 | -65, 173 | -65, 174 | -65, 175 | -64, 176 | -64, 177 | -64, 178 | -64, 179 | -64, 180 | -63, 181 | -63, 182 | -63, 183 | -63, 184 | -63, 185 | -63, 186 | -63, 187 | -62, 188 | -62, 189 | -62, 190 | -62, 191 | -62, 192 | -62, 193 | -62, 194 | -62, 195 | -61, 196 | -61, 197 | -61, 198 | -61, 199 | -61, 200 | -60, 201 | -60, 202 | -60, 203 | -60, 204 | -60, 205 | -60, 206 | -59, 207 | -59, 208 | -59, 209 | -59, 210 | -59, 211 | -59, 212 | -58, 213 | -58, 214 | -58, 215 | -58, 216 | -58, 217 | -57, 218 | -57, 219 | -56, 220 | -56, 221 | -56, 222 | -56, 223 | -56, 224 | -56, 225 | -55, 226 | -55, 227 | -55, 228 | -55, 229 | -54, 230 | -54, 231 | -54, 232 | -54, 233 | -54, 234 | -54, 235 | -53, 236 | -53, 237 | -53, 238 | -52, 239 | -52, 240 | -52, 241 | -52, 242 | -52, 243 | -52, 244 | -52, 245 | -52, 246 | -51, 247 | -51, 248 | -51, 249 | -50, 250 | -50, 251 | -50, 252 | -49, 253 | -49, 254 | -49, 255 | -49, 256 | -48, 257 | -48, 258 | -48, 259 | -48, 260 | -48, 261 | -48, 262 | -47, 263 | -47, 264 | -47, 265 | -47, 266 | -47, 267 | -46, 268 | -46, 269 | -46, 270 | -46, 271 | -46, 272 | -46, 273 | -45, 274 | -45, 275 | -45, 276 | -45, 277 | -45, 278 | -45, 279 | -45, 280 | -45, 281 | -44, 282 | -44, 283 | -44, 284 | -43, 285 | -43, 286 | -43, 287 | -43, 288 | -43, 289 | -43, 290 | -42, 291 | -42, 292 | -42, 293 | -41, 294 | -41, 295 | -41, 296 | -41, 297 | -41, 298 | -41, 299 | -41, 300 | -41, 301 | -41, 302 | -41, 303 | -41, 304 | -41, 305 | -40, 306 | -40, 307 | -40, 308 | -40, 309 | -40, 310 | -39, 311 | -39, 312 | -38, 313 | -38, 314 | -38, 315 | -38, 316 | -38, 317 | -38, 318 | -38, 319 | -37, 320 | -37, 321 | -37, 322 | -36, 323 | -36, 324 | -35, 325 | -35, 326 | -35, 327 | -35, 328 | -35, 329 | -35, 330 | -35, 331 | -35, 332 | -35, 333 | -35, 334 | -34, 335 | -34, 336 | -34, 337 | -34, 338 | -34, 339 | -33, 340 | -33, 341 | -33, 342 | -33, 343 | -32, 344 | -32, 345 | -32, 346 | -32, 347 | -32, 348 | -31, 349 | -31, 350 | -31, 351 | -31, 352 | -31, 353 | -30, 354 | -30, 355 | -29, 356 | -29, 357 | -28, 358 | -28, 359 | -28, 360 | -28, 361 | -28, 362 | -28, 363 | -28, 364 | -28, 365 | -28, 366 | -27, 367 | -27, 368 | -27, 369 | -27, 370 | -27, 371 | -27, 372 | -27, 373 | -27, 374 | -26, 375 | -26, 376 | -26, 377 | -26, 378 | -25, 379 | -25, 380 | -25, 381 | -25, 382 | -24, 383 | -24, 384 | -24, 385 | -24, 386 | -24, 387 | -23, 388 | -23, 389 | -23, 390 | -23, 391 | -23, 392 | -23, 393 | -23, 394 | -22, 395 | -22, 396 | -22, 397 | -22, 398 | -20, 399 | -20, 400 | -20, 401 | -20, 402 | -19, 403 | -19, 404 | -19, 405 | -19, 406 | -18, 407 | -17, 408 | -17, 409 | -16, 410 | -16, 411 | -16, 412 | -16, 413 | -16, 414 | -15, 415 | -15, 416 | -15, 417 | -15, 418 | -14, 419 | -14, 420 | -14, 421 | -13, 422 | -13, 423 | -12, 424 | -12, 425 | -12, 426 | -12, 427 | -12, 428 | -12, 429 | -12, 430 | -12, 431 | -11, 432 | -11, 433 | -10, 434 | -10, 435 | -10, 436 | -10, 437 | -10, 438 | -10, 439 | -10, 440 | -10, 441 | -10, 442 | -10, 443 | -10, 444 | -9, 445 | -9, 446 | -9, 447 | -9, 448 | -9, 449 | -9, 450 | -9, 451 | -9, 452 | -8, 453 | -7, 454 | -7, 455 | -6, 456 | -6, 457 | -6, 458 | -6, 459 | -6, 460 | -5, 461 | -5, 462 | -5, 463 | -5, 464 | -4, 465 | -4, 466 | -4, 467 | -4, 468 | -4, 469 | -4, 470 | -4, 471 | -3, 472 | -3, 473 | -3, 474 | -2, 475 | -2, 476 | -2, 477 | -2, 478 | -1, 479 | -1, 480 | -1, 481 | 0, 482 | 0, 483 | 0, 484 | 0, 485 | 0, 486 | 0, 487 | 0, 488 | 1, 489 | 1, 490 | 1, 491 | 1, 492 | 1, 493 | 2, 494 | 2, 495 | 2, 496 | 2, 497 | 2, 498 | 2, 499 | 3, 500 | 3, 501 | 3, 502 | 3, 503 | 4, 504 | 4, 505 | 4, 506 | 4, 507 | 4, 508 | 4, 509 | 4, 510 | 4, 511 | 5, 512 | 5, 513 | 5, 514 | 6, 515 | 6, 516 | 6, 517 | 6, 518 | 7, 519 | 7, 520 | 7, 521 | 8, 522 | 8, 523 | 8, 524 | 8, 525 | 8, 526 | 8, 527 | 8, 528 | 8, 529 | 8, 530 | 8, 531 | 8, 532 | 8, 533 | 9, 534 | 9, 535 | 9, 536 | 9, 537 | 9, 538 | 9, 539 | 9, 540 | 9, 541 | 9, 542 | 10, 543 | 10, 544 | 10, 545 | 10, 546 | 10, 547 | 10, 548 | 11, 549 | 11, 550 | 11, 551 | 11, 552 | 12, 553 | 12, 554 | 12, 555 | 12, 556 | 12, 557 | 12, 558 | 13, 559 | 13, 560 | 13, 561 | 13, 562 | 13, 563 | 13, 564 | 14, 565 | 14, 566 | 14, 567 | 14, 568 | 14, 569 | 14, 570 | 15, 571 | 15, 572 | 15, 573 | 16, 574 | 17, 575 | 17, 576 | 18, 577 | 18, 578 | 18, 579 | 18, 580 | 18, 581 | 18, 582 | 19, 583 | 19, 584 | 19, 585 | 19, 586 | 19, 587 | 19, 588 | 19, 589 | 19, 590 | 19, 591 | 20, 592 | 20, 593 | 20, 594 | 20, 595 | 20, 596 | 20, 597 | 20, 598 | 21, 599 | 21, 600 | 21, 601 | 22, 602 | 22, 603 | 22, 604 | 22, 605 | 22, 606 | 22, 607 | 23, 608 | 23, 609 | 23, 610 | 23, 611 | 23, 612 | 23, 613 | 23, 614 | 23, 615 | 23, 616 | 24, 617 | 24, 618 | 24, 619 | 24, 620 | 25, 621 | 25, 622 | 25, 623 | 25, 624 | 25, 625 | 25, 626 | 25, 627 | 25, 628 | 26, 629 | 26, 630 | 26, 631 | 26, 632 | 26, 633 | 26, 634 | 26, 635 | 26, 636 | 26, 637 | 27, 638 | 27, 639 | 27, 640 | 27, 641 | 27, 642 | 28, 643 | 28, 644 | 28, 645 | 28, 646 | 28, 647 | 28, 648 | 29, 649 | 29, 650 | 29, 651 | 29, 652 | 30, 653 | 30, 654 | 30, 655 | 31, 656 | 31, 657 | 31, 658 | 31, 659 | 31, 660 | 31, 661 | 31, 662 | 31, 663 | 32, 664 | 32, 665 | 32, 666 | 32, 667 | 32, 668 | 33, 669 | 33, 670 | 33, 671 | 34, 672 | 34, 673 | 34, 674 | 35, 675 | 35, 676 | 35, 677 | 35, 678 | 36, 679 | 36, 680 | 36, 681 | 36, 682 | 36, 683 | 36, 684 | 37, 685 | 37, 686 | 38, 687 | 38, 688 | 38, 689 | 38, 690 | 38, 691 | 39, 692 | 39, 693 | 39, 694 | 39, 695 | 39, 696 | 40, 697 | 40, 698 | 40, 699 | 40, 700 | 41, 701 | 41, 702 | 41, 703 | 42, 704 | 42, 705 | 42, 706 | 42, 707 | 42, 708 | 43, 709 | 43, 710 | 43, 711 | 43, 712 | 43, 713 | 43, 714 | 44, 715 | 44, 716 | 44, 717 | 44, 718 | 45, 719 | 45, 720 | 45, 721 | 46, 722 | 46, 723 | 46, 724 | 46, 725 | 46, 726 | 47, 727 | 47, 728 | 47, 729 | 47, 730 | 47, 731 | 48, 732 | 48, 733 | 48, 734 | 48, 735 | 49, 736 | 49, 737 | 49, 738 | 49, 739 | 49, 740 | 50, 741 | 50, 742 | 50, 743 | 50, 744 | 50, 745 | 50, 746 | 51, 747 | 51, 748 | 51, 749 | 52, 750 | 52, 751 | 52, 752 | 52, 753 | 52, 754 | 53, 755 | 53, 756 | 54, 757 | 54, 758 | 54, 759 | 54, 760 | 54, 761 | 54, 762 | 54, 763 | 54, 764 | 54, 765 | 55, 766 | 55, 767 | 55, 768 | 55, 769 | 56, 770 | 56, 771 | 56, 772 | 56, 773 | 56, 774 | 57, 775 | 57, 776 | 57, 777 | 57, 778 | 58, 779 | 58, 780 | 58, 781 | 58, 782 | 58, 783 | 59, 784 | 59, 785 | 59, 786 | 59, 787 | 59, 788 | 59, 789 | 60, 790 | 61, 791 | 61, 792 | 61, 793 | 61, 794 | 61, 795 | 61, 796 | 62, 797 | 62, 798 | 62, 799 | 62, 800 | 63, 801 | 63, 802 | 63, 803 | 63, 804 | 63, 805 | 63, 806 | 64, 807 | 64, 808 | 65, 809 | 65, 810 | 65, 811 | 65, 812 | 65, 813 | 65, 814 | 65, 815 | 66, 816 | 66, 817 | 66, 818 | 66, 819 | 67, 820 | 67, 821 | 68, 822 | 68, 823 | 68, 824 | 69, 825 | 69, 826 | 69, 827 | 69, 828 | 69, 829 | 69, 830 | 69, 831 | 70, 832 | 70, 833 | 70, 834 | 70, 835 | 70, 836 | 71, 837 | 71, 838 | 71, 839 | 71, 840 | 71, 841 | 72, 842 | 72, 843 | 72, 844 | 73, 845 | 73, 846 | 73, 847 | 73, 848 | 74, 849 | 74, 850 | 74, 851 | 74, 852 | 74, 853 | 75, 854 | 75, 855 | 75, 856 | 75, 857 | 75, 858 | 75, 859 | 75, 860 | 75, 861 | 75, 862 | 75, 863 | 75, 864 | 75, 865 | 75, 866 | 75, 867 | 76, 868 | 76, 869 | 76, 870 | 76, 871 | 76, 872 | 77, 873 | 77, 874 | 77, 875 | 78, 876 | 78, 877 | 78, 878 | 78, 879 | 78, 880 | 78, 881 | 78, 882 | 79, 883 | 79, 884 | 79, 885 | 79, 886 | 79, 887 | 79, 888 | 79, 889 | 80, 890 | 80, 891 | 80, 892 | 81, 893 | 81, 894 | 81, 895 | 82, 896 | 82, 897 | 82, 898 | 82, 899 | 82, 900 | 82, 901 | 82, 902 | 82, 903 | 82, 904 | 82, 905 | 83, 906 | 83, 907 | 83, 908 | 83, 909 | 84, 910 | 84, 911 | 84, 912 | 84, 913 | 84, 914 | 85, 915 | 85, 916 | 85, 917 | 85, 918 | 85, 919 | 86, 920 | 86, 921 | 87, 922 | 87, 923 | 87, 924 | 87, 925 | 87, 926 | 88, 927 | 89, 928 | 89, 929 | 89, 930 | 89, 931 | 89, 932 | 89, 933 | 89, 934 | 89, 935 | 89, 936 | 90, 937 | 90, 938 | 90, 939 | 90, 940 | 90, 941 | 90, 942 | 90, 943 | 90, 944 | 90, 945 | 91, 946 | 91, 947 | 91, 948 | 91, 949 | 91, 950 | 91, 951 | 91, 952 | 91, 953 | 92, 954 | 92, 955 | 92, 956 | 93, 957 | 93, 958 | 93, 959 | 93, 960 | 93, 961 | 93, 962 | 94, 963 | 94, 964 | 94, 965 | 94, 966 | 94, 967 | 94, 968 | 94, 969 | 94, 970 | 94, 971 | 95, 972 | 95, 973 | 95, 974 | 95, 975 | 95, 976 | 95, 977 | 96, 978 | 96, 979 | 96, 980 | 96, 981 | 96, 982 | 97, 983 | 97, 984 | 97, 985 | 97, 986 | 97, 987 | 97, 988 | 98, 989 | 98, 990 | 98, 991 | 98, 992 | 98, 993 | 99, 994 | 99, 995 | 99, 996 | 100, 997 | 100, 998 | 100, 999 | 100, 1000 | 100, 1001 | 100 1002 | ]; 1003 | --------------------------------------------------------------------------------