mirror of https://github.com/nodejs/node.git
buffer: use slightly faster NaN check
PR-URL: https://github.com/nodejs/node/pull/12286 Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>pull/12286/head
parent
021719738e
commit
e0f0f2664e
|
@ -0,0 +1,23 @@
|
|||
'use strict';
|
||||
const common = require('../common.js');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
value: ['@'.charCodeAt(0)],
|
||||
n: [1e7]
|
||||
});
|
||||
|
||||
function main(conf) {
|
||||
const n = +conf.n;
|
||||
const search = +conf.value;
|
||||
const aliceBuffer = fs.readFileSync(
|
||||
path.resolve(__dirname, '../fixtures/alice.html')
|
||||
);
|
||||
|
||||
bench.start();
|
||||
for (var i = 0; i < n; i++) {
|
||||
aliceBuffer.indexOf(search, 0, undefined);
|
||||
}
|
||||
bench.end(n);
|
||||
}
|
|
@ -659,7 +659,8 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
|
|||
// Coerce to Number. Values like null and [] become 0.
|
||||
byteOffset = +byteOffset;
|
||||
// If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer.
|
||||
if (Number.isNaN(byteOffset)) {
|
||||
// `x !== x`-style conditionals are a faster form of `isNaN(x)`
|
||||
if (byteOffset !== byteOffset) {
|
||||
byteOffset = dir ? 0 : (buffer.length - 1);
|
||||
}
|
||||
dir = !!dir; // Cast to bool.
|
||||
|
@ -882,7 +883,8 @@ function adjustOffset(offset, length) {
|
|||
// Use Math.trunc() to convert offset to an integer value that can be larger
|
||||
// than an Int32. Hence, don't use offset | 0 or similar techniques.
|
||||
offset = Math.trunc(offset);
|
||||
if (offset === 0 || Number.isNaN(offset)) {
|
||||
// `x !== x`-style conditionals are a faster form of `isNaN(x)`
|
||||
if (offset === 0 || offset !== offset) {
|
||||
return 0;
|
||||
} else if (offset < 0) {
|
||||
offset += length;
|
||||
|
|
Loading…
Reference in New Issue