console.time shows different time running the same function

你说的曾经没有我的故事 提交于 2020-01-23 15:34:35

问题


I use console.time to show the time of the function. But I found that it shows different running time of the same function.

I have simplified my function as below:

const findIP = (res) => {
    let arr = []
    arr = res.split(',')
}
console.time('1')
findIP('1,2,3,4,5,6,7,8,9,0')
console.timeEnd('1')
console.time('2')
findIP('1,2,3,4,5,6,7,8,9,0')
console.timeEnd('2')

The time difference between the two is very large.

I have tried to run several times. And it still cost different time.


回答1:


To quote the answer in the the following link:

If you run shorten multiple times, the V8 engine has a JIT compiler that will optimize that piece of code so it runs faster the next time.

https://stackoverflow.com/a/54601440




回答2:


Try changing the argument value, for example

console.time('1')
findIP('1,2,3,4,5,6,7,8,9,0')
console.timeEnd('1')
console.time('2')
findIP('1,2,3,4,43,6,7,8,9,4')
console.timeEnd('2')

you will see approx equal time

Reason of that difference is: The browser cache

Simple Definition browser cache is a temporary storage area in memory or on disk that holds the most recently downloaded Web pages and/or calculated result.



来源:https://stackoverflow.com/questions/58235219/console-time-shows-different-time-running-the-same-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!