Skip to content

Instantly share code, notes, and snippets.

@MirzaLeka
Created January 21, 2023 17:00
Show Gist options
  • Save MirzaLeka/e6fd30bf7de9d94dd8afae1d947849da to your computer and use it in GitHub Desktop.
Save MirzaLeka/e6fd30bf7de9d94dd8afae1d947849da to your computer and use it in GitHub Desktop.
Benchmark JavaScript Code

Benchmark JavaScript Commands

Using console.time() & console.timeEnd() you can measure how long it takes for your code to execute.

console.time('any-name-you-want');

for (var i = 0; i < 1000_000; i++) {}

console.timeEnd('the-same-name-as-above')

Output: 4.10693359375 ms

How it works

You set console.time() before the code you want to benchmark. Then you write an actual code to benchmark. After the block of code you add console.timeEnd() that prints how much time has passed between console.time() and console.timeEnd().

console.time('loop');

for (var i = 0; i < 1000_000_000; i++) {}

console.timeEnd('loop')

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