diff options
author | Trevor Norris <trev.norris@gmail.com> | 2015-11-11 00:15:15 -0700 |
---|---|---|
committer | Trevor Norris <trev.norris@gmail.com> | 2015-12-17 17:29:16 -0700 |
commit | 36e8a2c63dc17d7f6e473d0ce5bfa67f8d0fe31d (patch) | |
tree | b6413988b2475a75045be2c735350f0a8cf860a1 /src/node.js | |
parent | 946315f7d81826f8edd4b9ada87f4285cf88539e (diff) | |
download | node-new-36e8a2c63dc17d7f6e473d0ce5bfa67f8d0fe31d.tar.gz |
node: improve performance of hrtime()
process.hrtime() was performing too many operations in C++ that could be
done faster in JS. Move those operations over by creating a length 4
Uint32Array and perform bitwise operations on the seconds so that it was
unnecessary for the native API to do any object creation or set any
fields.
This has improved performance from ~350 ns/op to ~65 ns/op. Light
benchmark included to demonstrate the performance change.
PR-URL: https://github.com/nodejs/node/pull/3780
Reviewed-By: Fedor Indutny <fedor@indutny.com>
Diffstat (limited to 'src/node.js')
-rw-r--r-- | src/node.js | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/node.js b/src/node.js index 9541932e85..51928e68ee 100644 --- a/src/node.js +++ b/src/node.js @@ -181,12 +181,27 @@ } startup.setupProcessObject = function() { + const _hrtime = process.hrtime; + const hrValues = new Uint32Array(3); + process._setupProcessObject(pushValueToArray); function pushValueToArray() { for (var i = 0; i < arguments.length; i++) this.push(arguments[i]); } + + 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]; + } + return ret; + }; }; startup.globalVariables = function() { |