Using the console.time
function
The console.time()
method starts a timer you can use to track how long an operation takes. You give each timer a unique name, and may have up to 10,000 timers running on a given page. When you call console.timeEnd()
with the same name, the browser will output the time, in milliseconds, that elapsed since the timer was started.
To implement this, simply call console.time at the top of your script:
console.time("program");
Then call console.time at the bottom of the script:
console.timeEnd("program");
The output is a console.log
resembling:
program: 1123ms
UNIX time command
If you're not working in a browser environment (I.E. Node.js) and you're on a UNIX system, you can install and use the time command, which can be used as a prefix to any other command and informs you of performance of a program in real time, user time, and sys time.
time node path/to/javascript/file.js
Which should output:
# program output...
real 0m0.123s
user 0m0.123s
sys 0m0.123s
FireFox developer tools
Firefox Developer Tools, there's a performance tab that can be used to measure the performance of specific parts of a web page load time, from page load to specific resource load times, to program execution speeds.
To open it:
- Press
ctrl + shift + k
on your keyboard - Select the
performance
tab - Press the "Start recording" button
- Press the "Capture profile" button
This should then open a new tab at profiler.firefox.com showing you all of the performance information of that site. From frame-by-frame captures of the page load in progress, to the exact amount of milliseconds the page took to load, to the exact CPU resources needed to load the page.
The profiler.firefox.com page includes all of the performance graphs, charts, information, properties and data you'll ever need.
And if you like the Firefox performance profiler, try the Firefox Browser Developer Edition for even more dev-tools.
Honorable mention: Using online benchmarks
Notice: I've rewritten this section because JSPerf went down.
JSBen is a great tool for testing the performance of small(er) snippets, simply visit the site input the first sample, and the second and JSBen will tell you which benchmark was more performant.