Initial commit
commit
4a5b494f7c
@ -0,0 +1,5 @@
|
|||||||
|
# time-call
|
||||||
|
|
||||||
|
Measures the execution time of a chunk of code to nanosecond precision, while also providing access to its return value.
|
||||||
|
|
||||||
|
Proper docs coming Eventually(tm), see example.js in the repo for now.
|
@ -0,0 +1,17 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
const timeCall = require("./");
|
||||||
|
|
||||||
|
let dummyArray = new Array(20000).fill(0);
|
||||||
|
|
||||||
|
let { value, time } = timeCall(() => {
|
||||||
|
for (let i = 0; i < 1e3; i++) {
|
||||||
|
dummyArray = dummyArray.map((value) => value + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dummyArray;
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(time); // 386335402
|
||||||
|
console.log(value); // [ 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, ...
|
||||||
|
|
@ -0,0 +1,19 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
function hrtimeToNanoseconds(time) {
|
||||||
|
// If the numbers here become big enough to cause loss of precision, we probably have bigger issues than numeric precision...
|
||||||
|
return (time[0] * 1e9) + time[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = function measureTime(func) {
|
||||||
|
// let startTime = process.hrtime.bigint();
|
||||||
|
let startTime = hrtimeToNanoseconds(process.hrtime());
|
||||||
|
let result = func();
|
||||||
|
// let endTime = process.hrtime.bigint();
|
||||||
|
let endTime = hrtimeToNanoseconds(process.hrtime());
|
||||||
|
|
||||||
|
return {
|
||||||
|
value: result,
|
||||||
|
time: (endTime - startTime)
|
||||||
|
};
|
||||||
|
};
|
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"name": "time-call",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"main": "index.js",
|
||||||
|
"repository": "https://git.cryto.net/joepie91/time-call.git",
|
||||||
|
"author": "Sven Slootweg <admin@cryto.net>",
|
||||||
|
"license": "WTFPL OR CC0-1.0"
|
||||||
|
}
|
Loading…
Reference in New Issue