@dcmjs-org/dcmjs 1.0.0-beta.6 → 1.0.0-beta.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/dcmjs.js CHANGED
@@ -7738,13 +7738,34 @@
7738
7738
  }
7739
7739
  return createBuffer.buffer;
7740
7740
  }
7741
+
7742
+ /**
7743
+ * Returns the index of the buffer holding byte `start`, or undefined if
7744
+ * no buffer holds it. Buffers are only ever appended, so offsets[] is
7745
+ * increasing and a binary search finds it. A linear scan here made
7746
+ * writing a sequence of many items quadratic, since every write near the
7747
+ * end of a stream scanned all of its buffers.
7748
+ * https://github.com/dcmjs-org/dcmjs/issues/629
7749
+ */
7741
7750
  }, {
7742
7751
  key: "findStart",
7743
7752
  value: function findStart() {
7744
7753
  var start = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
7745
- for (var index = 0; index < this.buffers.length; index++) {
7746
- if (start >= this.offsets[index] && start < this.offsets[index] + this.lengths[index]) {
7747
- return index;
7754
+ // Refuses NaN and negatives; NaN fails both comparisons below and
7755
+ // would otherwise be reported as found.
7756
+ if (!(start >= 0)) {
7757
+ return;
7758
+ }
7759
+ var low = 0;
7760
+ var high = this.offsets.length - 1;
7761
+ while (low <= high) {
7762
+ var mid = low + high >> 1;
7763
+ if (start < this.offsets[mid]) {
7764
+ high = mid - 1;
7765
+ } else if (start >= this.offsets[mid] + this.lengths[mid]) {
7766
+ low = mid + 1;
7767
+ } else {
7768
+ return mid;
7748
7769
  }
7749
7770
  }
7750
7771
  }