New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
console.time measuring lot of time the first time that is being executed #27549
Comments
I am able to reproduce this and it's one of the reasons why I always measure the same thing at least twice and take the second example instead of the first. Looking just at the code does not bring up anything obviously wrong though. I guess we have to check closer what's actually executed the first and the second time and we should probably also check the function compiled and optimized or not. |
Perhaps some kind of initialization is happening? Adding a |
@mscdex Are you also on macOS or something else? |
I tested on Linux. |
Testing with different versions. Looks like the first call always took a bit of time in all versions until 11, where the difference is bit high:
Edit: I'm on macOS. |
What I did below may offer some clues. Actually const t1 = process.hrtime()
const t2 = process.hrtime()
const t3 = process.hrtime()
const t4 = process.hrtime()
const t5 = process.hrtime()
console.log('node interval 1: ', t2[1] - t1[1], 'nanos')
console.log('node interval 2: ', t3[1] - t2[1], 'nanos')
console.log('node interval 3: ', t4[1] - t3[1], 'nanos')
console.log('node interval 4: ', t5[1] - t4[1], 'nanos') node interval 1: 110704 nanos
node interval 2: 13197 nanos
node interval 3: 6347 nanos
node interval 4: 5509 nanos It seems that the first three calls do something time-consuming. As #include <stdio.h>
#include <mach/mach_time.h>
#include <inttypes.h>
int main() {
static mach_timebase_info_data_t info;
mach_timebase_info(&info);
u_int64_t t1 = mach_absolute_time() * info.numer / info.denom;
u_int64_t t2 = mach_absolute_time() * info.numer / info.denom;
u_int64_t t3 = mach_absolute_time() * info.numer / info.denom;
u_int64_t t4 = mach_absolute_time() * info.numer / info.denom;
u_int64_t t5 = mach_absolute_time() * info.numer / info.denom;
printf("native interval 1: %" PRIu64 " nanos\n", t2 - t1);
printf("native interval 2: %" PRIu64 " nanos\n", t3 - t2);
printf("native interval 3: %" PRIu64 " nanos\n", t4 - t3);
printf("native interval 4: %" PRIu64 " nanos\n", t5 - t4);
} native interval 1: 43 nanos
native interval 2: 42 nanos
native interval 3: 44 nanos
native interval 4: 42 nanos The intervals between calls are normal and stable. So I think node may do something time-consuming in the first 2 or 3 calls of internal binding module, other methods should have the same problem. I pick static void CPUUsage(const FunctionCallbackInfo<Value>& args) {
uint64_t t = uv_hrtime();
printf("cpu usage time: %" PRIu64 "\n", t);
// omit...
} Make several calls from javascript. process.cpuUsage()
process.cpuUsage()
process.cpuUsage()
process.cpuUsage()
process.cpuUsage()
process.cpuUsage()
process.cpuUsage() Output cpu usage time: 47534149403985
cpu usage time: 47534149533758
cpu usage time: 47534149592956
cpu usage time: 47534149613795
cpu usage time: 47534149622485
cpu usage time: 47534149629442
cpu usage time: 47534149636316 Intervals between calls
So it may be a common problem with internal binding module, but I didn't figure out the truth yet... |
This is (largely) caused by V8's lazy compilation feature: V8 doesn't actually compile JS code until first use. Compare:
With:
The difference with I'm going to close the issue but leave a comment if you think there's something that can be done to improve the status quo. |
The first time that console.time function is used, it is measuring more time than the following calls. This is a minimum code to reproduce the issue:
It results in:
This impact directly in time measurement for any function executed in node and can lead to false results.
Any idea about?
The text was updated successfully, but these errors were encountered: