diff --git a/index.js b/index.js index 8cb261de..76a2e618 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,8 @@ const customInspectSymbol = ? Symbol['for']('nodejs.util.inspect.custom') // eslint-disable-line dot-notation : null +const decoderUTF8 = new TextDecoder('utf8') + exports.Buffer = Buffer exports.SlowBuffer = SlowBuffer exports.INSPECT_MAX_BYTES = 50 @@ -970,7 +972,14 @@ function base64Slice (buf, start, end) { } } +// For smaller buffers than this TextDecoder#decode appears +// to have more overhead than doing it in Javascript directly. +const TEXT_DECODER_THRESHOLD = 7000 + function utf8Slice (buf, start, end) { + if ((end - start) > TEXT_DECODER_THRESHOLD) { + return decoderUTF8.decode(buf.slice(start, end)) + } end = Math.min(buf.length, end) const res = [] diff --git a/perf/readUtf8.js b/perf/readUtf8.js index a0109ff8..e78db9bc 100644 --- a/perf/readUtf8.js +++ b/perf/readUtf8.js @@ -5,15 +5,25 @@ var suite = util.suite() // 256 random bytes var array = [ 152, 130, 206, 23, 243, 238, 197, 44, 27, 86, 208, 36, 163, 184, 164, 21, 94, 242, 178, 46, 25, 26, 253, 178, 72, 147, 207, 112, 236, 68, 179, 190, 29, 83, 239, 147, 125, 55, 143, 19, 157, 68, 157, 58, 212, 224, 150, 39, 128, 24, 94, 225, 120, 121, 75, 192, 112, 19, 184, 142, 203, 36, 43, 85, 26, 147, 227, 139, 242, 186, 57, 78, 11, 102, 136, 117, 180, 210, 241, 92, 3, 215, 54, 167, 249, 1, 44, 225, 146, 86, 2, 42, 68, 21, 47, 238, 204, 153, 216, 252, 183, 66, 222, 255, 15, 202, 16, 51, 134, 1, 17, 19, 209, 76, 238, 38, 76, 19, 7, 103, 249, 5, 107, 137, 64, 62, 170, 57, 16, 85, 179, 193, 97, 86, 166, 196, 36, 148, 138, 193, 210, 69, 187, 38, 242, 97, 195, 219, 252, 244, 38, 1, 197, 18, 31, 246, 53, 47, 134, 52, 105, 72, 43, 239, 128, 203, 73, 93, 199, 75, 222, 220, 166, 34, 63, 236, 11, 212, 76, 243, 171, 110, 78, 39, 205, 204, 6, 177, 233, 212, 243, 0, 33, 41, 122, 118, 92, 252, 0, 157, 108, 120, 70, 137, 100, 223, 243, 171, 232, 66, 126, 111, 142, 33, 3, 39, 117, 27, 107, 54, 1, 217, 227, 132, 13, 166, 3, 73, 53, 127, 225, 236, 134, 219, 98, 214, 125, 148, 24, 64, 142, 111, 231, 194, 42, 150, 185, 10, 182, 163, 244, 19, 4, 59, 135, 16 ] -var browserBuffer = new BrowserBuffer(array) -var nodeBuffer = new Buffer(array) +function addTest(size) { + let arr = array; + for(var i = 0; i < size; i++) { + arr = arr.concat(array); + } -suite - .add('BrowserBuffer#readUtf8', function () { - browserBuffer.toString() - }) + var browserBuffer = BrowserBuffer.from(arr) + var nodeBuffer = Buffer.from(arr) + suite + .add('BrowserBuffer#readUtf8 ' + nodeBuffer.byteLength, function () { + browserBuffer.toString() + }) -if (!process.browser) suite - .add('NodeBuffer#readUtf8', function () { - nodeBuffer.toString() - }) + if (!process.browser) suite + .add('NodeBuffer#readUtf8 ' + nodeBuffer.byteLength, function () { + nodeBuffer.toString() + }) +} + +for(var i = 0; i < 6; i++) { + addTest(i * 10); +} \ No newline at end of file