console.time()

 

-사용 방법

console.time('time');
for(var i=0; i<50000; i++){}
console.timeEnd('time');

-결과

time: 0.971923828125 ms

 

-참고 URL : https://developer.mozilla.org/en-US/docs/Web/API/console/time

 

console.time() - Web APIs | MDN

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 out

developer.mozilla.org

 

Performance.now()

 

-사용방법

var time0 = performance.now();
for(var i=0; i<50000; i++){}
var time1 = performance.now();
console.log("time: " + (time1 - time0) + ' ms');

-결과

time: 0.7999999523162842 ms

-참고 URL : https://developer.mozilla.org/en-US/docs/Web/API/Performance

 

Performance - Web APIs | MDN

The Performance interface provides access to performance-related information for the current page. It's part of the High Resolution Time API, but is enhanced by the Performance Timeline API, the Navigation Timing API, the User Timing API, and the Resource

developer.mozilla.org

 

console.time() , performance.now() 비교

console.time('console.time()');
for(var i=0; i<50000; i++){}
console.timeEnd('console.time()');

var time0 = performance.now();
for(var i=0; i<50000; i++){}
var time1 = performance.now();
console.log("performance.now(): " + (time1 - time0) + ' ms');
console.time(): 0.965087890625 ms
performance.now(): 0.7999999523162842 ms

결과를 보면 performance.now()이 더 정확하게 나오는 걸 확인이 가능합니다.

대신 간단하게 쓸 수 있는 건 console.time()입니다.

 

performance은 .mark() , .measure() 등등 많은 기능이 있으니 참고 URL에서 확인하고 사용하시면 됩니다.

+ Recent posts