DEV Community

Discussion on: JavaScript Performance using console.time() & console.timeEnd()

 
dimpiax profile image
Dmytro Pylypenko

It depends on skills of developer, if he cannot write vanilla simple functions and doesn't know how to organize architecture and behavior of application, which produces polluting – then developer starts to use 3rd party libraries for trivial purposes.
If developer is skilled – it's enough to wrap using native functions, like console.time and console.timeEnd.

For example:

const measure = (label, closure) => { 
    console.time(label); 
    closure(); 
    console.timeEnd(label); 
}

measure('one', () => { 
    Array.from(Array(10000), (_, i) => i).map(v => Math.random(v)); 
})
Thread Thread
 
varoman profile image
varo manukyan • Edited

Please note, every time you re-run it will produce completely different results, so you can't event say how long it will take a particular function to run.

Unlike console.time(), benchmark allows you to run you suits lots of times, giving your more correct average results.

Or you can test your units with jsperf.com/

Thread Thread
 
dimpiax profile image
Dmytro Pylypenko

Of course, because processor allocates resources differently.
When we talk about measurement for unit tests or for performance function – time is the range of allowed min/max.

Thread Thread
 
bgadrian profile image
Adrian B.G.

I agree, the dev must know what a framework does, but framework or vanilla, the LOC used to benchmark (like performance or console) would be better to be in separate .js files, which are not bundled into the product.