简体   繁体   English

节点中的时间优先调用功能

[英]Time first call function in node

I have the following code: 我有以下代码:

 let startTime; let stopTime; const arr = [1, 2, 3, 8, 5, 0, 110, 4, 4, 16, 3, 8, 7, 56, 1, 2, 3, 8, 5, 0, 110, 16, 3, 8, 7, 56]; const sum = 63; durationTime = (start, stop, desc) => { let duration = (stop - start); console.info('Duration' + ((desc !== undefined) ? '(' + desc + ')' : '') + ': ' + duration + 'ms'); }; findPair = (arr, sum) => { let result = []; const filterArr = arr.filter((number) => { return number <= sum; }); filterArr.forEach((valueFirst, index) => { for (let i = index + 1; i < filterArr.length; i++) { const valueSecond = filterArr[i]; const checkSum = valueFirst + valueSecond; if (sum === checkSum) { result.push([valueFirst, valueSecond]); } } }); //console.info(result); }; for (let i = 0; i < 5; i++) { startTime = new Date(); findPair(arr, sum); stopTime = new Date(); durationTime(startTime, stopTime); } 

When I run locally on the nodejs (v8.9.3), the result in the console: 当我在nodejs(v8.9.3)上本地运行时,结果在控制台中:

Duration(0): 4ms 持续时间(0):4ms

Duration(1): 0ms 持续时间(1):0ms

Duration(2): 0ms 持续时间(2):0ms

Duration(3): 0ms 持续时间(3):0ms

Duration(4): 0ms 持续时间(4):0ms

My Question: Why does the first call of 'findPair' take 4ms and other calls only 0ms? 我的问题:为什么“ findPair”的第一次呼叫需要4毫秒,而其他呼叫仅需要0毫秒?

When the loop runs the first time the JavaScript engine (Google's V8) interprets the code, compiles it and executes. 当循环首次运行时,JavaScript引擎(Google的V8)会解释代码,对其进行编译并执行。 However, code that runs more often will have it's compiled code optimized and cached so that subsequent runs of that code can run faster. 但是,运行频率更高的代码将优化并缓存其编译后的代码,以使该代码的后续运行可以更快地运行。 Code inside loops would be a good example of such code that runs often. 循环内的代码将是经常运行的此类代码的一个很好的示例。

Unless you fiddle with prototypes and things that could make that cached code invalid, it'll keep running that cached code which is a lot faster than interpreting the code every time it runs. 除非您摆弄原型和可能使缓存的代码无效的东西,否则它将继续运行缓存的代码,这比每次运行时解释代码都要快得多。

There are a lot of smart things V8 does to make your code run faster, if you are interested in this stuff I'd highly recommend reading the sources for my answer: V8有很多聪明的事情可以使您的代码更快地运行,如果您对此内容感兴趣,我强烈建议您阅读答案的源代码:

Dynamic Memory and V8 with JavaScript 动态内存和带有JavaScript的V8

How JavaScript works: inside the V8 engine + 5 tips on how to write optimized code JavaScript的工作原理:V8引擎内部+ 5条有关编写优化代码的技巧

Before beginning, better time measurement is: 开始之前,更好的时间度量是:

for (let i = 0; i < 5; i++) {
    console.time('timer '+i);
    findPair(arr, sum);
    console.timeEnd('timer ' + i);
}

... ...

The first function call is slower, probably because V8 dynamically create hidden classes behind the scenes, and (initially) puts function into it. 第一次调用函数的速度较慢,这可能是因为V8在后台动态创建了隐藏的类,并且(最初)将函数放入了其中。

More information on https://github.com/v8/v8/wiki/Design%20Elements#fast-property-access 有关https://github.com/v8/v8/wiki/Design%20Elements#fast-property-access的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM