| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,26 +4,80 @@ const v8 = require('v8'); | |||
| 4 | 4 | ||
| 5 | 5 | const bench = common.createBenchmark(main, { | |
| 6 | 6 | size: [16, 512, 1024, 4096, 16386], | |
| 7 | + args: [1, 2, 3, 4, 5], | ||
| 7 | 8 | millions: [1] | |
| 8 | 9 | }); | |
| 9 | 10 | ||
| 10 | 11 | function main(conf) { | |
| 11 | 12 | const iter = (conf.millions >>> 0) * 1e6; | |
| 12 | 13 | const size = (conf.size >>> 0); | |
| 13 | - const b0 = new Buffer(size).fill('a'); | ||
| 14 | - const b1 = new Buffer(size).fill('a'); | ||
| 14 | + const args = (conf.args >>> 0); | ||
| 15 | + const b0 = Buffer.alloc(size, 'a'); | ||
| 16 | + const b1 = Buffer.alloc(size, 'a'); | ||
| 17 | + const b0Len = b0.length; | ||
| 18 | + const b1Len = b1.length; | ||
| 19 | + var i; | ||
| 15 | 20 | ||
| 16 | 21 | b1[size - 1] = 'b'.charCodeAt(0); | |
| 17 | 22 | ||
| 18 | 23 | // Force optimization before starting the benchmark | |
| 19 | - b0.compare(b1); | ||
| 24 | + switch (args) { | ||
| 25 | + case 2: | ||
| 26 | + b0.compare(b1, 0); | ||
| 27 | + break; | ||
| 28 | + case 3: | ||
| 29 | + b0.compare(b1, 0, b1Len); | ||
| 30 | + break; | ||
| 31 | + case 4: | ||
| 32 | + b0.compare(b1, 0, b1Len, 0); | ||
| 33 | + break; | ||
| 34 | + case 5: | ||
| 35 | + b0.compare(b1, 0, b1Len, 0, b0Len); | ||
| 36 | + break; | ||
| 37 | + default: | ||
| 38 | + b0.compare(b1); | ||
| 39 | + } | ||
| 20 | 40 | v8.setFlagsFromString('--allow_natives_syntax'); | |
| 21 | 41 | eval('%OptimizeFunctionOnNextCall(b0.compare)'); | |
| 22 | - b0.compare(b1); | ||
| 23 | - | ||
| 24 | - bench.start(); | ||
| 25 | - for (var i = 0; i < iter; i++) { | ||
| 26 | - b0.compare(b1); | ||
| 42 | + switch (args) { | ||
| 43 | + case 2: | ||
| 44 | + b0.compare(b1, 0); | ||
| 45 | + bench.start(); | ||
| 46 | + for (i = 0; i < iter; i++) { | ||
| 47 | + b0.compare(b1, 0); | ||
| 48 | + } | ||
| 49 | + bench.end(iter / 1e6); | ||
| 50 | + break; | ||
| 51 | + case 3: | ||
| 52 | + b0.compare(b1, 0, b1Len); | ||
| 53 | + bench.start(); | ||
| 54 | + for (i = 0; i < iter; i++) { | ||
| 55 | + b0.compare(b1, 0, b1Len); | ||
| 56 | + } | ||
| 57 | + bench.end(iter / 1e6); | ||
| 58 | + break; | ||
| 59 | + case 4: | ||
| 60 | + b0.compare(b1, 0, b1Len, 0); | ||
| 61 | + bench.start(); | ||
| 62 | + for (i = 0; i < iter; i++) { | ||
| 63 | + b0.compare(b1, 0, b1Len, 0); | ||
| 64 | + } | ||
| 65 | + bench.end(iter / 1e6); | ||
| 66 | + break; | ||
| 67 | + case 5: | ||
| 68 | + b0.compare(b1, 0, b1Len, 0, b0Len); | ||
| 69 | + bench.start(); | ||
| 70 | + for (i = 0; i < iter; i++) { | ||
| 71 | + b0.compare(b1, 0, b1Len, 0, b0Len); | ||
| 72 | + } | ||
| 73 | + bench.end(iter / 1e6); | ||
| 74 | + break; | ||
| 75 | + default: | ||
| 76 | + b0.compare(b1); | ||
| 77 | + bench.start(); | ||
| 78 | + for (i = 0; i < iter; i++) { | ||
| 79 | + b0.compare(b1); | ||
| 80 | + } | ||
| 81 | + bench.end(iter / 1e6); | ||
| 27 | 82 | } | |
| 28 | - bench.end(iter / 1e6); | ||
| 29 | 83 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | 'use strict'; | |
| 3 | 3 | ||
| 4 | 4 | const binding = process.binding('buffer'); | |
| 5 | + const { compare: compare_, compareOffset } = binding; | ||
| 5 | 6 | const { isArrayBuffer, isSharedArrayBuffer } = process.binding('util'); | |
| 6 | 7 | const bindingObj = {}; | |
| 7 | 8 | const internalUtil = require('internal/util'); | |
@@ -529,36 +530,43 @@ Buffer.prototype.compare = function compare(target, | |||
| 529 | 530 | ||
| 530 | 531 | if (!(target instanceof Buffer)) | |
| 531 | 532 | throw new TypeError('Argument must be a Buffer'); | |
| 533 | + if (arguments.length === 1) | ||
| 534 | + return compare_(this, target); | ||
| 532 | 535 | ||
| 533 | 536 | if (start === undefined) | |
| 534 | 537 | start = 0; | |
| 538 | + else if (start < 0) | ||
| 539 | + throw new RangeError('out of range index'); | ||
| 540 | + else | ||
| 541 | + start >>>= 0; | ||
| 542 | + | ||
| 535 | 543 | if (end === undefined) | |
| 536 | 544 | end = target.length; | |
| 545 | + else if (end > target.length) | ||
| 546 | + throw new RangeError('out of range index'); | ||
| 547 | + else | ||
| 548 | + end >>>= 0; | ||
| 549 | + | ||
| 537 | 550 | if (thisStart === undefined) | |
| 538 | 551 | thisStart = 0; | |
| 552 | + else if (thisStart < 0) | ||
| 553 | + throw new RangeError('out of range index'); | ||
| 554 | + else | ||
| 555 | + thisStart >>>= 0; | ||
| 556 | + | ||
| 539 | 557 | if (thisEnd === undefined) | |
| 540 | 558 | thisEnd = this.length; | |
| 541 | - | ||
| 542 | - if (start < 0 || | ||
| 543 | - end > target.length || | ||
| 544 | - thisStart < 0 || | ||
| 545 | - thisEnd > this.length) { | ||
| 559 | + else if (thisEnd > this.length) | ||
| 546 | 560 | throw new RangeError('out of range index'); | |
| 547 | - } | ||
| 561 | + else | ||
| 562 | + thisEnd >>>= 0; | ||
| 548 | 563 | ||
| 549 | - if (thisStart >= thisEnd && start >= end) | ||
| 550 | - return 0; | ||
| 551 | 564 | if (thisStart >= thisEnd) | |
| 552 | - return -1; | ||
| 553 | - if (start >= end) | ||
| 565 | + return (start >= end ? 0 : -1); | ||
| 566 | + else if (start >= end) | ||
| 554 | 567 | return 1; | |
| 555 | 568 | ||
| 556 | - start >>>= 0; | ||
| 557 | - end >>>= 0; | ||
| 558 | - thisStart >>>= 0; | ||
| 559 | - thisEnd >>>= 0; | ||
| 560 | - | ||
| 561 | - return binding.compareOffset(this, target, start, thisStart, end, thisEnd); | ||
| 569 | + return compareOffset(this, target, start, thisStart, end, thisEnd); | ||
| 562 | 570 | }; | |
| 563 | 571 | ||
| 564 | 572 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments