Last active
January 24, 2017 19:22
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Next - make this reusable throughout the code