點燈坊

失くすものさえない今が強くなるチャンスよ

Using console.time to Measure the Performance

Sam Xiao's Avatar 2021-11-29

If we care about the performance of some code snippets, we can use console.time to measure the performance.

Version

ECMAScript 2015

console.time

time000

It shows the execution time about the code snippet.

<script setup>
let data = [
  { title: 'Macbook', price: 100 },
  { title: 'iPhone', price: 200 },
  { title: 'iPad', price: 300 }
]

console.time ('Apple')
for (let x of data)
  console.log (`${x.title}: ${x.price}`)
console.timeEnd ('Apple')
</script>
  • Use console.time to create a measurement with a label
  • Use console.timeEnd to end measurement with a label

Conclusion

  • The usages between console.time and console.group are very similar, but we have to provide a label to console.timeEnd to end the measurement

Reference

MDN, console.time
MDN, console.timeEnd