r100732 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100731‎ | r100732 | r100733 >
Date:19:24, 25 October 2011
Author:catrope
Status:deferred
Tags:
Comment:
Factor out the batched-splice-apply code into es.spliceArray(), fix a logic error in the code, and add a test case
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/es.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js (modified) (history)
  • /trunk/parsers/wikidom/tests/hype/es.test.js (added) (history)
  • /trunk/parsers/wikidom/tests/hype/index.html (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/tests/hype/es.test.js
@@ -0,0 +1,14 @@
 2+module( 'es' );
 3+
 4+test( 'es.spliceArray', 1, function() {
 5+ var insert = [], i, arr = ['foo', 'bar'], expected = [];
 6+ expected[0] = 'foo';
 7+ for ( i = 0; i < 3000; i++ ) {
 8+ insert[i] = i;
 9+ expected[i + 1] = i;
 10+ }
 11+ expected[3001] = 'bar';
 12+
 13+ es.spliceArray( arr, 1, insert );
 14+ deepEqual( arr, expected, 'splicing 3000 elements into the middle of a 2-element array' );
 15+} );
Property changes on: trunk/parsers/wikidom/tests/hype/es.test.js
___________________________________________________________________
Added: svn:eol-style
116 + native
Index: trunk/parsers/wikidom/tests/hype/index.html
@@ -26,6 +26,7 @@
2727 <script src="../../lib/hype/models/es.TableCellModel.js"></script>
2828 <script src="../../lib/hype/models/es.TableModel.js"></script>
2929 <script src="../../lib/hype/models/es.TableRowModel.js"></script>
 30+ <script src="es.test.js"></script>
3031 <script src="es.ModelNode.test.js"></script>
3132 <script src="es.DocumentModel.test.js"></script>
3233 </body>
Index: trunk/parsers/wikidom/lib/hype/es.js
@@ -119,3 +119,26 @@
120120 }
121121 return destination;
122122 };
 123+
 124+/**
 125+ * Splice one array into another. This is the equivalent of arr.splice( offset, 0, i1, i2, i3, ... )
 126+ * except that i1, i2, i3, ... are specified as an array rather than separate parameters.
 127+ *
 128+ * @static
 129+ * @method
 130+ * @param arr {Array} Array to splice insertion into. Will be modified
 131+ * @param offset {Number} Offset in arr to splice insertion in at. May be negative; see the 'index' parameter for Array.prototype.splice()
 132+ * @param insertion {Array} Array to insert
 133+ */
 134+es.spliceArray = function( arr, offset, insertion ) {
 135+ // We need to splice insertion in in batches, because of parameter list length limits which vary cross-browser.
 136+ // 1024 seems to be a safe batch size on all browsers.
 137+ var index = 0, batchSize = 1024;
 138+ while ( index < insertion.length ) {
 139+ // Call arr.splice( offset, 0, i0, i1, i2, ..., i1023 );
 140+ arr.splice.apply(
 141+ arr, [index + offset, 0].concat( insertion.slice( index, index + batchSize ) )
 142+ );
 143+ index += batchSize;
 144+ }
 145+};
Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js
@@ -79,16 +79,7 @@
8080 }
8181
8282 function insert( op ) {
83 - // Splice content into document in 1024 element batches, as to not overflow max allowed
84 - // arguments, which apply is limited by
85 - var index = 0,
86 - batchSize = 1024;
87 - while ( index < op.data.length ) {
88 - this.data.splice.apply(
89 - this.data, [this.cursor, 0].concat( op.data.slice( index, index + batchSize ) )
90 - );
91 - index += batchSize;
92 - }
 83+ es.spliceArray( this.data, this.cursor, op.data );
9384 annotate.call( this, this.cursor + op.data.length );
9485 this.cursor += op.data.length;
9586 }

Status & tagging log