Skip to content

Instantly share code, notes, and snippets.

@iSkore
Last active January 24, 2017 19:22
Show Gist options
  • Save iSkore/2c8d445ca83c84eebf0a1e9e155abb25 to your computer and use it in GitHub Desktop.
Save iSkore/2c8d445ca83c84eebf0a1e9e155abb25 to your computer and use it in GitHub Desktop.
Extremely fast logging system. Put this at the TOP of any script and call `log()` when you're complete.
"use strict";
// Average completion time: 9.57 ms
const [ a, o, ms, s, log ] = ( function * () {
yield * [
( process.hrtime )(),
process.hrtime,
ms => ( ( ms[ 0 ] * 1e9 + ms[ 1 ] ) / 1000000 ),
s => s / 1000,
() => {
const f = o( a ), msf = ms( f ), sf = s( msf );
return { a, o: f, ms: msf, s: sf };
}
];
} )();
setTimeout( () => {
console.log( `Benchmark results ${JSON.stringify( log(), null, 4 )}` );
}, 1000 ); // add a second
// And in a clean class
"use strict";
class Time {
constructor( name ) {
this.name = name = name || "time";
const
ms = ms => ( ( ms[ 0 ] * 1e9 + ms[ 1 ] ) / 1000000 ),
s = s => s / 1000,
c = process.hrtime();
this.start = () => {
const a = process.hrtime();
this.start = a;
};
this.stop = () => {
const o = process.hrtime( this.start ), msf = ms( o ), sf = s( msf );
this.stop = o;
this.ms = msf;
this.s = sf;
this.time = name + ": " + msf;
};
this.creationTime = ms( process.hrtime( c ) );
}
}
let x = new Time( "name" );
x.start();
x.stop();
console.log( x );
console.log( x.ms );
@iSkore
Copy link
Author

iSkore commented Oct 1, 2016

Next - make this reusable throughout the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment