Skip to content

Commit 78fd435

Browse files
committed
node: improve performance of process.hrtime()
Move argument validation out of C++ and into JS. Improves performance by about 15-20%. PR-URL: #4484 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent bfa925f commit 78fd435

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/node.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,18 +2138,8 @@ void Kill(const FunctionCallbackInfo<Value>& args) {
21382138
// and nanoseconds, to avoid any integer overflow possibility.
21392139
// Pass in an Array from a previous hrtime() call to instead get a time diff.
21402140
void Hrtime(const FunctionCallbackInfo<Value>& args) {
2141-
Environment* env = Environment::GetCurrent(args);
2142-
21432141
uint64_t t = uv_hrtime();
21442142

2145-
if (!args[1]->IsUndefined()) {
2146-
if (!args[1]->IsArray()) {
2147-
return env->ThrowTypeError(
2148-
"process.hrtime() only accepts an Array tuple");
2149-
}
2150-
args.GetReturnValue().Set(true);
2151-
}
2152-
21532143
Local<ArrayBuffer> ab = args[0].As<Uint32Array>()->Buffer();
21542144
uint32_t* fields = static_cast<uint32_t*>(ab->GetContents().Data());
21552145

src/node.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,23 @@
192192
}
193193

194194
process.hrtime = function hrtime(ar) {
195-
const ret = [0, 0];
196-
if (_hrtime(hrValues, ar)) {
197-
ret[0] = (hrValues[0] * 0x100000000 + hrValues[1]) - ar[0];
198-
ret[1] = hrValues[2] - ar[1];
199-
} else {
200-
ret[0] = hrValues[0] * 0x100000000 + hrValues[1];
201-
ret[1] = hrValues[2];
195+
_hrtime(hrValues);
196+
197+
if (typeof ar !== 'undefined') {
198+
if (Array.isArray(ar)) {
199+
return [
200+
(hrValues[0] * 0x100000000 + hrValues[1]) - ar[0],
201+
hrValues[2] - ar[1]
202+
];
203+
}
204+
205+
throw new TypeError('process.hrtime() only accepts an Array tuple');
202206
}
203-
return ret;
207+
208+
return [
209+
hrValues[0] * 0x100000000 + hrValues[1],
210+
hrValues[2]
211+
];
204212
};
205213
};
206214

0 commit comments

Comments
 (0)