|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Benchmark = require('benchmark'); |
| 4 | +const benchmarks = require('beautify-benchmark'); |
| 5 | +const assert = require('assert'); |
| 6 | + |
| 7 | +const ByteBuffer = require('byte'); |
| 8 | +const io = ByteBuffer.allocate(1024 * 1024); |
| 9 | + |
| 10 | +const str = '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234'; |
| 11 | +io.putRawString(str); |
| 12 | +const buf = io.array(); |
| 13 | + |
| 14 | +// io.position(0); |
| 15 | +// console.log(io.getRawStringByStringLength(1024)); |
| 16 | +// console.log(buf.toString()); |
| 17 | + |
| 18 | +io.position(0); |
| 19 | +assert(io.getRawStringByStringLength(1024) === buf.toString()); |
| 20 | + |
| 21 | +function getUTF(buf) { |
| 22 | + const data = []; |
| 23 | + const length = buf.length; |
| 24 | + for (let i = 0; i < length; i++) { |
| 25 | + const ch = buf[i]; |
| 26 | + if (ch < 0x80) { |
| 27 | + data.push(ch); |
| 28 | + } else if ((ch & 0xe0) === 0xc0) { |
| 29 | + const ch1 = buf[++i]; |
| 30 | + const v = ((ch & 0x1f) << 6) + (ch1 & 0x3f); |
| 31 | + data.push(v); |
| 32 | + } else if ((ch & 0xf0) === 0xe0) { |
| 33 | + const ch1 = buf[++i]; |
| 34 | + const ch2 = buf[++i]; |
| 35 | + const v = ((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f); |
| 36 | + data.push(v); |
| 37 | + } else { |
| 38 | + throw new Error('string is not valid UTF-8 encode'); |
| 39 | + } |
| 40 | + } |
| 41 | + return String.fromCharCode.apply(String, data); |
| 42 | +} |
| 43 | + |
| 44 | +assert(getUTF(buf) === buf.toString()); |
| 45 | + |
| 46 | + |
| 47 | +function getUTF2(buf) { |
| 48 | + const length = buf.length; |
| 49 | + const data = []; |
| 50 | + let start = 0; |
| 51 | + const numInts = length >> 2; |
| 52 | + for (let i = 0; i < numInts; i++) { |
| 53 | + const num = buf.readInt32BE(i * 4); |
| 54 | + if ((num & 0x80808080) !== 0) { |
| 55 | + throw new Error(); |
| 56 | + } |
| 57 | + } |
| 58 | + const offset = start + length; |
| 59 | + return buf.toString('utf8', 0, offset); |
| 60 | + |
| 61 | + // while (i < length) { |
| 62 | + // const num = buf.readInt32BE(i); |
| 63 | + // if ((num & 0x80808080) === 0) { |
| 64 | + // data.push(buf[i]); |
| 65 | + // data.push(buf[i + 1]); |
| 66 | + // data.push(buf[i + 2]); |
| 67 | + // data.push(buf[i + 3]); |
| 68 | + // i += 4; |
| 69 | + // } else { |
| 70 | + // assert(false); |
| 71 | + // } |
| 72 | + // } |
| 73 | + // return String.fromCharCode.apply(String, data); |
| 74 | +} |
| 75 | + |
| 76 | +assert(getUTF2(buf) === buf.toString()); |
| 77 | +io.position(0); |
| 78 | +assert(io.getRawStringFast(1024) === buf.toString()); |
| 79 | + |
| 80 | +const suite = new Benchmark.Suite(); |
| 81 | +suite |
| 82 | + .add('io.getRawStringByStringLength', function() { |
| 83 | + io._offset = 0; |
| 84 | + io.getRawStringByStringLength(1024); |
| 85 | + }) |
| 86 | + .add('io.getRawStringFast', function() { |
| 87 | + io._offset = 0; |
| 88 | + io.getRawStringFast(1024); |
| 89 | + }) |
| 90 | + .add('buf.toString', function() { |
| 91 | + buf.toString(); |
| 92 | + }) |
| 93 | + .add('getUTF', function() { |
| 94 | + getUTF(buf); |
| 95 | + }) |
| 96 | + .add('getUTF2', function() { |
| 97 | + getUTF2(buf); |
| 98 | + }) |
| 99 | + .on('cycle', function(event) { |
| 100 | + benchmarks.add(event.target); |
| 101 | + }) |
| 102 | + .on('start', function(event) { |
| 103 | + console.log('\n Cache Benchmark\n node version: %s, date: %s\n Starting...', |
| 104 | + process.version, Date()); |
| 105 | + }) |
| 106 | + .on('complete', function done() { |
| 107 | + benchmarks.log(); |
| 108 | + }) |
| 109 | + .run({ 'async': false }); |
| 110 | + |
| 111 | +// Cache Benchmark |
| 112 | +// node version: v8.9.0, date: Thu Nov 16 2017 13:26:18 GMT+0800 (CST) |
| 113 | +// Starting... |
| 114 | +// 2 tests completed. |
| 115 | +// |
| 116 | +// with cache x 21,724 ops/sec ±2.01% (82 runs sampled) |
| 117 | +// without cache x 8,523 ops/sec ±1.17% (89 runs sampled) |
0 commit comments