Skip to content

Commit

Permalink
Use performance.now for browser
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Jun 1, 2017
1 parent 92ce0c2 commit 36bdcde
Show file tree
Hide file tree
Showing 3 changed files with 1,613 additions and 2 deletions.
26 changes: 25 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,34 @@ function nanoseconds() {
return Math.floor(Math.random() * 1e6);
}

function customHrtime(time) {
function browserHrtime() {
const current = Math.floor(window.performance.now() * 1e6);
const currentSeconds = Math.floor(current / 1e9);
const currentNanoseconds = current % 1e9;
return [
currentSeconds,
currentNanoseconds,
];
}

function getHrtime() {
/* eslint no-undef:0 */
if (window && window.performance && window.performance.now) {
return browserHrtime();
}
const current = Date.now();
const currentSeconds = Math.floor(current / 1000);
const currentNanoseconds = ((current % 1000) * 1e6) + nanoseconds();
return [
currentSeconds,
currentNanoseconds,
];
}

function customHrtime(time) {
const arr = getHrtime();
const currentSeconds = arr[0];
const currentNanoseconds = arr[1];
if (!time) {
return [
currentSeconds,
Expand Down

0 comments on commit 36bdcde

Please sign in to comment.