| 28 | }); |
| 29 | |
| 30 | async function start() { |
| 31 | const result = { |
| 32 | writeComparison: {}, |
| 33 | toStringComparison: {}, |
| 34 | } |
| 35 | |
| 36 | { |
| 37 | const platformBufferA = new BrowserBuffer(jsonString.length); |
| 38 | const platformBufferB = new BrowserBuffer(jsonString.length); |
| 39 | const nativeBuffer = Buffer.alloc(jsonString.length); |
| 40 | |
| 41 | var suite = new Benchmark.Suite(); |
| 42 | suite |
| 43 | .add("browser utf8Write", function () { |
| 44 | platformBufferA.utf8Write(jsonString, 0); |
| 45 | }) |
| 46 | .add("browser write", function () { |
| 47 | platformBufferB.write(jsonString, 0, 'utf8'); |
| 48 | }) |
| 49 | .add("native write", function () { |
| 50 | nativeBuffer.write(jsonString, 0, 'utf8'); |
| 51 | }) |
| 52 | .on("complete", function (e) { |
| 53 | e.currentTarget.forEach(({ name, hz }) => { |
| 54 | result.writeComparison[name] = Math.ceil(hz); |
| 55 | }); |
| 56 | }) |
| 57 | .run({ async: false }); |
| 58 | console.log("Write operation per second") |
| 59 | console.table(result.writeComparison); |
| 60 | } |
| 61 | |
| 62 | { |
| 63 | const browserBuffer = new BrowserBuffer(jsonString.length); |
| 64 | const nativeBuffer = Buffer.alloc(jsonString.length); |
| 65 | browserBuffer.write(jsonString, 0, 'utf8'); |
| 66 | nativeBuffer.write(jsonString, 0, 'utf8'); |
| 67 | |
| 68 | var suite = new Benchmark.Suite(); |
| 69 | suite |
| 70 | .add("browser toString", function () { |
| 71 | browserBuffer.toString('utf8', 0, jsonString.length); |
| 72 | }) |
| 73 | .add("native toString", function () { |
| 74 | nativeBuffer.toString('utf8', 0, jsonString.length); |
| 75 | }) |
| 76 | .on("complete", function (e) { |
| 77 | e.currentTarget.forEach(({ name, hz }) => { |
| 78 | result.toStringComparison[name] = Math.ceil(hz); |
| 79 | }); |
| 80 | }) |
| 81 | .run({ async: false }); |
| 82 | console.log("toString operation per second") |
| 83 | console.table(result.toStringComparison); |
| 84 | } |
| 85 | |
| 86 | const args = ['platform-buffer-draw.py', result.writeComparison['browser utf8Write'], result.writeComparison['browser write'], result.writeComparison['native write'], result.toStringComparison['browser toString'], result.toStringComparison['native toString']]; |
| 87 | |