JSFiddle - React, Tailwind, and code Playground

by Meir Gabay

HTML

<div id="myEle">

</div>


<table id='dates'>
  
</table>

<div id='time'>

</div>

JavaScript

Date.prototype.addDays = function(days)
{
    var dat = new Date(this.valueOf());
    dat.setDate(dat.getDate() + days);
    return dat;
}


var myArr = [];
var myDate = new Date();
var tempDate = null;
for (var i=0 ; i < 1000 ; i++){

	tempDate = myDate.addDays(i);
  tempDate = tempDate.getFullYear()  + "/" +(tempDate.getMonth()+1) + "/" + tempDate.getDate() ;
  tempDate = new Date(tempDate);
	myArr.push(tempDate.valueOf());
}

var myTable = document.getElementById('dates');
var myNode = null;
var myTr = null;
var row = null;
var cell = null;
myArr.forEach(function(val,index){
	 row = myTable.insertRow(index);//myNode = document.createTextNode("<td>" + val + "</td>");
  cell = row.insertCell(0);
  cell.innerHTML = val;
});

function binaryIndexOf(searchElement) {
	'use strict';

	var minIndex = 0;
	var maxIndex = this.length - 1;
	var currentIndex;
	var currentElement;
	var resultIndex;

	while (minIndex <= maxIndex) {
		resultIndex = currentIndex = (minIndex + maxIndex) / 2 | 0;
		currentElement = this[currentIndex];

		if (currentElement < searchElement) {
			minIndex = currentIndex + 1;
		}
		else if (currentElement > searchElement) {
			maxIndex = currentIndex - 1;
		}
		else {
			return currentIndex;
		}
	}

	return ~maxIndex;
}

Array.prototype.binaryIndexOf = binaryIndexOf;

var searchValue = 1485295200000;

var t0 = performance.now();

console.log(myArr.binaryIndexOf(searchValue));

var t1 = performance.now();
console.log((t1 - t0) + " milliseconds");


var t2 = performance.now();

myArr.forEach(function(value,index){
  if(value === searchValue ){
    console.log(index);
    return;
  }
});

var t3 = performance.now();

console.log((t3 - t2) + " milliseconds");

//console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")

//document.getElementById('time').innerText = (t1 - t0) + " milliseconds";