Which methods do you prefer to test the speed of your Javascript, and why? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 10 days ago.
Improve this questionI was simply wondering what methods folks are using to test their javascript's execution speed and look out for performance bottlenecks, whether they simply be code snipp开发者_StackOverflowets or services.
I'd also like to know why you would advocate this method. Flexibility? Simplicity? Or perhaps there is a single, right way. Whatever it is, I'm looking for the best method to use and why you'd argue it as such.
(Perhaps this is a question that should be a community wiki? Or just altogether inappropriate as being too subjective? But I'm searching for well-articulated examples that illustrate why certain options would be used, and I will mark the best answer accordingly.)
JSPerf.com is a nice site for JS benchmarking. It's used more for benchmarking different methodologies rather than pieces of your own code (but nothing is stopping you from using it to benchmark your code).
There's a SO discussion on this that I think you will find useful: How do you performance test JavaScript code?.
I just started playing with Firebug's Profiler and it looks pretty slick. Try that out and maybe it's what you're looking for.
You could also try using console.time and console.timeEnd in your js. It'll look something like this:
console.time('yourTimer');
$('.some-div').hover(function() {
$(this).slideUp();
}, function() {
$(this).slideDown();
});
console.timeEnd('yourTimer');
timer.js. It's just a simple timer, but on Google Chrome, you get microsecond resolution instead of normal JavaScript millisecond resolution.
Note: Simplest implementation of pure functional, ES6 method, no extra variables needed, just 3 lines of codes. Handles sync and async codes, so no external libraries needed, works in both JavaScript and Node JS, can even use to test the latency of your APIs
// Create one-liner timer function
let [timer, timingMonitor] = [0, () => timer = !timer ? Date.now() : `${Date.now() - timer}ms`]
// Initiate timer
timingMonitor();
// Your code here
doSomething();
// Capture and store the elapsed time
const timeElapsed = timingMonitor();
console.log(timeElapsed);
// Console output: "102ms", for example
精彩评论