diff options
author | Evan Lucas <evanlucas@me.com> | 2015-12-30 10:01:06 -0600 |
---|---|---|
committer | Evan Lucas <evanlucas@me.com> | 2015-12-30 16:52:11 -0600 |
commit | 78fd43514f1dc5144c71f9dbc2be3f6698da5522 (patch) | |
tree | 8c73e9084be9cbb885e259045fc51650636c9a6b /src | |
parent | bfa925f15f20949e02325a58b8cf3ffd1f188077 (diff) | |
download | node-new-78fd43514f1dc5144c71f9dbc2be3f6698da5522.tar.gz |
node: improve performance of process.hrtime()
Move argument validation out of C++ and into JS. Improves performance
by about 15-20%.
PR-URL: https://github.com/nodejs/node/pull/4484
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/node.cc | 10 | ||||
-rw-r--r-- | src/node.js | 24 |
2 files changed, 16 insertions, 18 deletions
diff --git a/src/node.cc b/src/node.cc index 894c8f7641..29127fbfc6 100644 --- a/src/node.cc +++ b/src/node.cc @@ -2138,18 +2138,8 @@ void Kill(const FunctionCallbackInfo<Value>& args) { // and nanoseconds, to avoid any integer overflow possibility. // Pass in an Array from a previous hrtime() call to instead get a time diff. void Hrtime(const FunctionCallbackInfo<Value>& args) { - Environment* env = Environment::GetCurrent(args); - uint64_t t = uv_hrtime(); - if (!args[1]->IsUndefined()) { - if (!args[1]->IsArray()) { - return env->ThrowTypeError( - "process.hrtime() only accepts an Array tuple"); - } - args.GetReturnValue().Set(true); - } - Local<ArrayBuffer> ab = args[0].As<Uint32Array>()->Buffer(); uint32_t* fields = static_cast<uint32_t*>(ab->GetContents().Data()); diff --git a/src/node.js b/src/node.js index 51928e68ee..428e55ea76 100644 --- a/src/node.js +++ b/src/node.js @@ -192,15 +192,23 @@ } process.hrtime = function hrtime(ar) { - const ret = [0, 0]; - if (_hrtime(hrValues, ar)) { - ret[0] = (hrValues[0] * 0x100000000 + hrValues[1]) - ar[0]; - ret[1] = hrValues[2] - ar[1]; - } else { - ret[0] = hrValues[0] * 0x100000000 + hrValues[1]; - ret[1] = hrValues[2]; + _hrtime(hrValues); + + if (typeof ar !== 'undefined') { + if (Array.isArray(ar)) { + return [ + (hrValues[0] * 0x100000000 + hrValues[1]) - ar[0], + hrValues[2] - ar[1] + ]; + } + + throw new TypeError('process.hrtime() only accepts an Array tuple'); } - return ret; + + return [ + hrValues[0] * 0x100000000 + hrValues[1], + hrValues[2] + ]; }; }; |