UNPKG

133 kBJavaScriptView Raw
1(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2(function (process,global){
3/*!
4 * Benchmark.js v1.0.0 <http://benchmarkjs.com/>
5 * Copyright 2010-2012 Mathias Bynens <http://mths.be/>
6 * Based on JSLitmus.js, copyright Robert Kieffer <http://broofa.com/>
7 * Modified by John-David Dalton <http://allyoucanleet.com/>
8 * Available under MIT license <http://mths.be/mit>
9 */
10;(function(window, undefined) {
11 'use strict';
12
13 /** Used to assign each benchmark an incrimented id */
14 var counter = 0;
15
16 /** Detect DOM document object */
17 var doc = isHostType(window, 'document') && document;
18
19 /** Detect free variable `define` */
20 var freeDefine = typeof define == 'function' &&
21 typeof define.amd == 'object' && define.amd && define;
22
23 /** Detect free variable `exports` */
24 var freeExports = typeof exports == 'object' && exports &&
25 (typeof global == 'object' && global && global == global.global && (window = global), exports);
26
27 /** Detect free variable `require` */
28 var freeRequire = typeof require == 'function' && require;
29
30 /** Used to crawl all properties regardless of enumerability */
31 var getAllKeys = Object.getOwnPropertyNames;
32
33 /** Used to get property descriptors */
34 var getDescriptor = Object.getOwnPropertyDescriptor;
35
36 /** Used in case an object doesn't have its own method */
37 var hasOwnProperty = {}.hasOwnProperty;
38
39 /** Used to check if an object is extensible */
40 var isExtensible = Object.isExtensible || function() { return true; };
41
42 /** Used to access Wade Simmons' Node microtime module */
43 var microtimeObject = req('microtime');
44
45 /** Used to access the browser's high resolution timer */
46 var perfObject = isHostType(window, 'performance') && performance;
47
48 /** Used to call the browser's high resolution timer */
49 var perfName = perfObject && (
50 perfObject.now && 'now' ||
51 perfObject.webkitNow && 'webkitNow'
52 );
53
54 /** Used to access Node's high resolution timer */
55 var processObject = isHostType(window, 'process') && process;
56
57 /** Used to check if an own property is enumerable */
58 var propertyIsEnumerable = {}.propertyIsEnumerable;
59
60 /** Used to set property descriptors */
61 var setDescriptor = Object.defineProperty;
62
63 /** Used to resolve a value's internal [[Class]] */
64 var toString = {}.toString;
65
66 /** Used to prevent a `removeChild` memory leak in IE < 9 */
67 var trash = doc && doc.createElement('div');
68
69 /** Used to integrity check compiled tests */
70 var uid = 'uid' + (+new Date);
71
72 /** Used to avoid infinite recursion when methods call each other */
73 var calledBy = {};
74
75 /** Used to avoid hz of Infinity */
76 var divisors = {
77 '1': 4096,
78 '2': 512,
79 '3': 64,
80 '4': 8,
81 '5': 0
82 };
83
84 /**
85 * T-Distribution two-tailed critical values for 95% confidence
86 * http://www.itl.nist.gov/div898/handbook/eda/section3/eda3672.htm
87 */
88 var tTable = {
89 '1': 12.706,'2': 4.303, '3': 3.182, '4': 2.776, '5': 2.571, '6': 2.447,
90 '7': 2.365, '8': 2.306, '9': 2.262, '10': 2.228, '11': 2.201, '12': 2.179,
91 '13': 2.16, '14': 2.145, '15': 2.131, '16': 2.12, '17': 2.11, '18': 2.101,
92 '19': 2.093, '20': 2.086, '21': 2.08, '22': 2.074, '23': 2.069, '24': 2.064,
93 '25': 2.06, '26': 2.056, '27': 2.052, '28': 2.048, '29': 2.045, '30': 2.042,
94 'infinity': 1.96
95 };
96
97 /**
98 * Critical Mann-Whitney U-values for 95% confidence
99 * http://www.saburchill.com/IBbiology/stats/003.html
100 */
101 var uTable = {
102 '5': [0, 1, 2],
103 '6': [1, 2, 3, 5],
104 '7': [1, 3, 5, 6, 8],
105 '8': [2, 4, 6, 8, 10, 13],
106 '9': [2, 4, 7, 10, 12, 15, 17],
107 '10': [3, 5, 8, 11, 14, 17, 20, 23],
108 '11': [3, 6, 9, 13, 16, 19, 23, 26, 30],
109 '12': [4, 7, 11, 14, 18, 22, 26, 29, 33, 37],
110 '13': [4, 8, 12, 16, 20, 24, 28, 33, 37, 41, 45],
111 '14': [5, 9, 13, 17, 22, 26, 31, 36, 40, 45, 50, 55],
112 '15': [5, 10, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59, 64],
113 '16': [6, 11, 15, 21, 26, 31, 37, 42, 47, 53, 59, 64, 70, 75],
114 '17': [6, 11, 17, 22, 28, 34, 39, 45, 51, 57, 63, 67, 75, 81, 87],
115 '18': [7, 12, 18, 24, 30, 36, 42, 48, 55, 61, 67, 74, 80, 86, 93, 99],
116 '19': [7, 13, 19, 25, 32, 38, 45, 52, 58, 65, 72, 78, 85, 92, 99, 106, 113],
117 '20': [8, 14, 20, 27, 34, 41, 48, 55, 62, 69, 76, 83, 90, 98, 105, 112, 119, 127],
118 '21': [8, 15, 22, 29, 36, 43, 50, 58, 65, 73, 80, 88, 96, 103, 111, 119, 126, 134, 142],
119 '22': [9, 16, 23, 30, 38, 45, 53, 61, 69, 77, 85, 93, 101, 109, 117, 125, 133, 141, 150, 158],
120 '23': [9, 17, 24, 32, 40, 48, 56, 64, 73, 81, 89, 98, 106, 115, 123, 132, 140, 149, 157, 166, 175],
121 '24': [10, 17, 25, 33, 42, 50, 59, 67, 76, 85, 94, 102, 111, 120, 129, 138, 147, 156, 165, 174, 183, 192],
122 '25': [10, 18, 27, 35, 44, 53, 62, 71, 80, 89, 98, 107, 117, 126, 135, 145, 154, 163, 173, 182, 192, 201, 211],
123 '26': [11, 19, 28, 37, 46, 55, 64, 74, 83, 93, 102, 112, 122, 132, 141, 151, 161, 171, 181, 191, 200, 210, 220, 230],
124 '27': [11, 20, 29, 38, 48, 57, 67, 77, 87, 97, 107, 118, 125, 138, 147, 158, 168, 178, 188, 199, 209, 219, 230, 240, 250],
125 '28': [12, 21, 30, 40, 50, 60, 70, 80, 90, 101, 111, 122, 132, 143, 154, 164, 175, 186, 196, 207, 218, 228, 239, 250, 261, 272],
126 '29': [13, 22, 32, 42, 52, 62, 73, 83, 94, 105, 116, 127, 138, 149, 160, 171, 182, 193, 204, 215, 226, 238, 249, 260, 271, 282, 294],
127 '30': [13, 23, 33, 43, 54, 65, 76, 87, 98, 109, 120, 131, 143, 154, 166, 177, 189, 200, 212, 223, 235, 247, 258, 270, 282, 293, 305, 317]
128 };
129
130 /**
131 * An object used to flag environments/features.
132 *
133 * @static
134 * @memberOf Benchmark
135 * @type Object
136 */
137 var support = {};
138
139 (function() {
140
141 /**
142 * Detect Adobe AIR.
143 *
144 * @memberOf Benchmark.support
145 * @type Boolean
146 */
147 support.air = isClassOf(window.runtime, 'ScriptBridgingProxyObject');
148
149 /**
150 * Detect if `arguments` objects have the correct internal [[Class]] value.
151 *
152 * @memberOf Benchmark.support
153 * @type Boolean
154 */
155 support.argumentsClass = isClassOf(arguments, 'Arguments');
156
157 /**
158 * Detect if in a browser environment.
159 *
160 * @memberOf Benchmark.support
161 * @type Boolean
162 */
163 support.browser = doc && isHostType(window, 'navigator');
164
165 /**
166 * Detect if strings support accessing characters by index.
167 *
168 * @memberOf Benchmark.support
169 * @type Boolean
170 */
171 support.charByIndex =
172 // IE 8 supports indexes on string literals but not string objects
173 ('x'[0] + Object('x')[0]) == 'xx';
174
175 /**
176 * Detect if strings have indexes as own properties.
177 *
178 * @memberOf Benchmark.support
179 * @type Boolean
180 */
181 support.charByOwnIndex =
182 // Narwhal, Rhino, RingoJS, IE 8, and Opera < 10.52 support indexes on
183 // strings but don't detect them as own properties
184 support.charByIndex && hasKey('x', '0');
185
186 /**
187 * Detect if Java is enabled/exposed.
188 *
189 * @memberOf Benchmark.support
190 * @type Boolean
191 */
192 support.java = isClassOf(window.java, 'JavaPackage');
193
194 /**
195 * Detect if the Timers API exists.
196 *
197 * @memberOf Benchmark.support
198 * @type Boolean
199 */
200 support.timeout = isHostType(window, 'setTimeout') && isHostType(window, 'clearTimeout');
201
202 /**
203 * Detect if functions support decompilation.
204 *
205 * @name decompilation
206 * @memberOf Benchmark.support
207 * @type Boolean
208 */
209 try {
210 // Safari 2.x removes commas in object literals
211 // from Function#toString results
212 // http://webk.it/11609
213 // Firefox 3.6 and Opera 9.25 strip grouping
214 // parentheses from Function#toString results
215 // http://bugzil.la/559438
216 support.decompilation = Function(
217 'return (' + (function(x) { return { 'x': '' + (1 + x) + '', 'y': 0 }; }) + ')'
218 )()(0).x === '1';
219 } catch(e) {
220 support.decompilation = false;
221 }
222
223 /**
224 * Detect ES5+ property descriptor API.
225 *
226 * @name descriptors
227 * @memberOf Benchmark.support
228 * @type Boolean
229 */
230 try {
231 var o = {};
232 support.descriptors = (setDescriptor(o, o, o), 'value' in getDescriptor(o, o));
233 } catch(e) {
234 support.descriptors = false;
235 }
236
237 /**
238 * Detect ES5+ Object.getOwnPropertyNames().
239 *
240 * @name getAllKeys
241 * @memberOf Benchmark.support
242 * @type Boolean
243 */
244 try {
245 support.getAllKeys = /\bvalueOf\b/.test(getAllKeys(Object.prototype));
246 } catch(e) {
247 support.getAllKeys = false;
248 }
249
250 /**
251 * Detect if own properties are iterated before inherited properties (all but IE < 9).
252 *
253 * @name iteratesOwnLast
254 * @memberOf Benchmark.support
255 * @type Boolean
256 */
257 support.iteratesOwnFirst = (function() {
258 var props = [];
259 function ctor() { this.x = 1; }
260 ctor.prototype = { 'y': 1 };
261 for (var prop in new ctor) { props.push(prop); }
262 return props[0] == 'x';
263 }());
264
265 /**
266 * Detect if a node's [[Class]] is resolvable (all but IE < 9)
267 * and that the JS engine errors when attempting to coerce an object to a
268 * string without a `toString` property value of `typeof` "function".
269 *
270 * @name nodeClass
271 * @memberOf Benchmark.support
272 * @type Boolean
273 */
274 try {
275 support.nodeClass = ({ 'toString': 0 } + '', toString.call(doc || 0) != '[object Object]');
276 } catch(e) {
277 support.nodeClass = true;
278 }
279 }());
280
281 /**
282 * Timer object used by `clock()` and `Deferred#resolve`.
283 *
284 * @private
285 * @type Object
286 */
287 var timer = {
288
289 /**
290 * The timer namespace object or constructor.
291 *
292 * @private
293 * @memberOf timer
294 * @type Function|Object
295 */
296 'ns': Date,
297
298 /**
299 * Starts the deferred timer.
300 *
301 * @private
302 * @memberOf timer
303 * @param {Object} deferred The deferred instance.
304 */
305 'start': null, // lazy defined in `clock()`
306
307 /**
308 * Stops the deferred timer.
309 *
310 * @private
311 * @memberOf timer
312 * @param {Object} deferred The deferred instance.
313 */
314 'stop': null // lazy defined in `clock()`
315 };
316
317 /** Shortcut for inverse results */
318 var noArgumentsClass = !support.argumentsClass,
319 noCharByIndex = !support.charByIndex,
320 noCharByOwnIndex = !support.charByOwnIndex;
321
322 /** Math shortcuts */
323 var abs = Math.abs,
324 floor = Math.floor,
325 max = Math.max,
326 min = Math.min,
327 pow = Math.pow,
328 sqrt = Math.sqrt;
329
330 /*--------------------------------------------------------------------------*/
331
332 /**
333 * The Benchmark constructor.
334 *
335 * @constructor
336 * @param {String} name A name to identify the benchmark.
337 * @param {Function|String} fn The test to benchmark.
338 * @param {Object} [options={}] Options object.
339 * @example
340 *
341 * // basic usage (the `new` operator is optional)
342 * var bench = new Benchmark(fn);
343 *
344 * // or using a name first
345 * var bench = new Benchmark('foo', fn);
346 *
347 * // or with options
348 * var bench = new Benchmark('foo', fn, {
349 *
350 * // displayed by Benchmark#toString if `name` is not available
351 * 'id': 'xyz',
352 *
353 * // called when the benchmark starts running
354 * 'onStart': onStart,
355 *
356 * // called after each run cycle
357 * 'onCycle': onCycle,
358 *
359 * // called when aborted
360 * 'onAbort': onAbort,
361 *
362 * // called when a test errors
363 * 'onError': onError,
364 *
365 * // called when reset
366 * 'onReset': onReset,
367 *
368 * // called when the benchmark completes running
369 * 'onComplete': onComplete,
370 *
371 * // compiled/called before the test loop
372 * 'setup': setup,
373 *
374 * // compiled/called after the test loop
375 * 'teardown': teardown
376 * });
377 *
378 * // or name and options
379 * var bench = new Benchmark('foo', {
380 *
381 * // a flag to indicate the benchmark is deferred
382 * 'defer': true,
383 *
384 * // benchmark test function
385 * 'fn': function(deferred) {
386 * // call resolve() when the deferred test is finished
387 * deferred.resolve();
388 * }
389 * });
390 *
391 * // or options only
392 * var bench = new Benchmark({
393 *
394 * // benchmark name
395 * 'name': 'foo',
396 *
397 * // benchmark test as a string
398 * 'fn': '[1,2,3,4].sort()'
399 * });
400 *
401 * // a test's `this` binding is set to the benchmark instance
402 * var bench = new Benchmark('foo', function() {
403 * 'My name is '.concat(this.name); // My name is foo
404 * });
405 */
406 function Benchmark(name, fn, options) {
407 var me = this;
408
409 // allow instance creation without the `new` operator
410 if (me == null || me.constructor != Benchmark) {
411 return new Benchmark(name, fn, options);
412 }
413 // juggle arguments
414 if (isClassOf(name, 'Object')) {
415 // 1 argument (options)
416 options = name;
417 }
418 else if (isClassOf(name, 'Function')) {
419 // 2 arguments (fn, options)
420 options = fn;
421 fn = name;
422 }
423 else if (isClassOf(fn, 'Object')) {
424 // 2 arguments (name, options)
425 options = fn;
426 fn = null;
427 me.name = name;
428 }
429 else {
430 // 3 arguments (name, fn [, options])
431 me.name = name;
432 }
433 setOptions(me, options);
434 me.id || (me.id = ++counter);
435 me.fn == null && (me.fn = fn);
436 me.stats = deepClone(me.stats);
437 me.times = deepClone(me.times);
438 }
439
440 /**
441 * The Deferred constructor.
442 *
443 * @constructor
444 * @memberOf Benchmark
445 * @param {Object} clone The cloned benchmark instance.
446 */
447 function Deferred(clone) {
448 var me = this;
449 if (me == null || me.constructor != Deferred) {
450 return new Deferred(clone);
451 }
452 me.benchmark = clone;
453 clock(me);
454 }
455
456 /**
457 * The Event constructor.
458 *
459 * @constructor
460 * @memberOf Benchmark
461 * @param {String|Object} type The event type.
462 */
463 function Event(type) {
464 var me = this;
465 return (me == null || me.constructor != Event)
466 ? new Event(type)
467 : (type instanceof Event)
468 ? type
469 : extend(me, { 'timeStamp': +new Date }, typeof type == 'string' ? { 'type': type } : type);
470 }
471
472 /**
473 * The Suite constructor.
474 *
475 * @constructor
476 * @memberOf Benchmark
477 * @param {String} name A name to identify the suite.
478 * @param {Object} [options={}] Options object.
479 * @example
480 *
481 * // basic usage (the `new` operator is optional)
482 * var suite = new Benchmark.Suite;
483 *
484 * // or using a name first
485 * var suite = new Benchmark.Suite('foo');
486 *
487 * // or with options
488 * var suite = new Benchmark.Suite('foo', {
489 *
490 * // called when the suite starts running
491 * 'onStart': onStart,
492 *
493 * // called between running benchmarks
494 * 'onCycle': onCycle,
495 *
496 * // called when aborted
497 * 'onAbort': onAbort,
498 *
499 * // called when a test errors
500 * 'onError': onError,
501 *
502 * // called when reset
503 * 'onReset': onReset,
504 *
505 * // called when the suite completes running
506 * 'onComplete': onComplete
507 * });
508 */
509 function Suite(name, options) {
510 var me = this;
511
512 // allow instance creation without the `new` operator
513 if (me == null || me.constructor != Suite) {
514 return new Suite(name, options);
515 }
516 // juggle arguments
517 if (isClassOf(name, 'Object')) {
518 // 1 argument (options)
519 options = name;
520 } else {
521 // 2 arguments (name [, options])
522 me.name = name;
523 }
524 setOptions(me, options);
525 }
526
527 /*--------------------------------------------------------------------------*/
528
529 /**
530 * Note: Some array methods have been implemented in plain JavaScript to avoid
531 * bugs in IE, Opera, Rhino, and Mobile Safari.
532 *
533 * IE compatibility mode and IE < 9 have buggy Array `shift()` and `splice()`
534 * functions that fail to remove the last element, `object[0]`, of
535 * array-like-objects even though the `length` property is set to `0`.
536 * The `shift()` method is buggy in IE 8 compatibility mode, while `splice()`
537 * is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9.
538 *
539 * In Opera < 9.50 and some older/beta Mobile Safari versions using `unshift()`
540 * generically to augment the `arguments` object will pave the value at index 0
541 * without incrimenting the other values's indexes.
542 * https://github.com/documentcloud/underscore/issues/9
543 *
544 * Rhino and environments it powers, like Narwhal and RingoJS, may have
545 * buggy Array `concat()`, `reverse()`, `shift()`, `slice()`, `splice()` and
546 * `unshift()` functions that make sparse arrays non-sparse by assigning the
547 * undefined indexes a value of undefined.
548 * https://github.com/mozilla/rhino/commit/702abfed3f8ca043b2636efd31c14ba7552603dd
549 */
550
551 /**
552 * Creates an array containing the elements of the host array followed by the
553 * elements of each argument in order.
554 *
555 * @memberOf Benchmark.Suite
556 * @returns {Array} The new array.
557 */
558 function concat() {
559 var value,
560 j = -1,
561 length = arguments.length,
562 result = slice.call(this),
563 index = result.length;
564
565 while (++j < length) {
566 value = arguments[j];
567 if (isClassOf(value, 'Array')) {
568 for (var k = 0, l = value.length; k < l; k++, index++) {
569 if (k in value) {
570 result[index] = value[k];
571 }
572 }
573 } else {
574 result[index++] = value;
575 }
576 }
577 return result;
578 }
579
580 /**
581 * Utility function used by `shift()`, `splice()`, and `unshift()`.
582 *
583 * @private
584 * @param {Number} start The index to start inserting elements.
585 * @param {Number} deleteCount The number of elements to delete from the insert point.
586 * @param {Array} elements The elements to insert.
587 * @returns {Array} An array of deleted elements.
588 */
589 function insert(start, deleteCount, elements) {
590 // `result` should have its length set to the `deleteCount`
591 // see https://bugs.ecmascript.org/show_bug.cgi?id=332
592 var deleteEnd = start + deleteCount,
593 elementCount = elements ? elements.length : 0,
594 index = start - 1,
595 length = start + elementCount,
596 object = this,
597 result = Array(deleteCount),
598 tail = slice.call(object, deleteEnd);
599
600 // delete elements from the array
601 while (++index < deleteEnd) {
602 if (index in object) {
603 result[index - start] = object[index];
604 delete object[index];
605 }
606 }
607 // insert elements
608 index = start - 1;
609 while (++index < length) {
610 object[index] = elements[index - start];
611 }
612 // append tail elements
613 start = index--;
614 length = max(0, (object.length >>> 0) - deleteCount + elementCount);
615 while (++index < length) {
616 if ((index - start) in tail) {
617 object[index] = tail[index - start];
618 } else if (index in object) {
619 delete object[index];
620 }
621 }
622 // delete excess elements
623 deleteCount = deleteCount > elementCount ? deleteCount - elementCount : 0;
624 while (deleteCount--) {
625 index = length + deleteCount;
626 if (index in object) {
627 delete object[index];
628 }
629 }
630 object.length = length;
631 return result;
632 }
633
634 /**
635 * Rearrange the host array's elements in reverse order.
636 *
637 * @memberOf Benchmark.Suite
638 * @returns {Array} The reversed array.
639 */
640 function reverse() {
641 var upperIndex,
642 value,
643 index = -1,
644 object = Object(this),
645 length = object.length >>> 0,
646 middle = floor(length / 2);
647
648 if (length > 1) {
649 while (++index < middle) {
650 upperIndex = length - index - 1;
651 value = upperIndex in object ? object[upperIndex] : uid;
652 if (index in object) {
653 object[upperIndex] = object[index];
654 } else {
655 delete object[upperIndex];
656 }
657 if (value != uid) {
658 object[index] = value;
659 } else {
660 delete object[index];
661 }
662 }
663 }
664 return object;
665 }
666
667 /**
668 * Removes the first element of the host array and returns it.
669 *
670 * @memberOf Benchmark.Suite
671 * @returns {Mixed} The first element of the array.
672 */
673 function shift() {
674 return insert.call(this, 0, 1)[0];
675 }
676
677 /**
678 * Creates an array of the host array's elements from the start index up to,
679 * but not including, the end index.
680 *
681 * @memberOf Benchmark.Suite
682 * @param {Number} start The starting index.
683 * @param {Number} end The end index.
684 * @returns {Array} The new array.
685 */
686 function slice(start, end) {
687 var index = -1,
688 object = Object(this),
689 length = object.length >>> 0,
690 result = [];
691
692 start = toInteger(start);
693 start = start < 0 ? max(length + start, 0) : min(start, length);
694 start--;
695 end = end == null ? length : toInteger(end);
696 end = end < 0 ? max(length + end, 0) : min(end, length);
697
698 while ((++index, ++start) < end) {
699 if (start in object) {
700 result[index] = object[start];
701 }
702 }
703 return result;
704 }
705
706 /**
707 * Allows removing a range of elements and/or inserting elements into the
708 * host array.
709 *
710 * @memberOf Benchmark.Suite
711 * @param {Number} start The start index.
712 * @param {Number} deleteCount The number of elements to delete.
713 * @param {Mixed} [val1, val2, ...] values to insert at the `start` index.
714 * @returns {Array} An array of removed elements.
715 */
716 function splice(start, deleteCount) {
717 var object = Object(this),
718 length = object.length >>> 0;
719
720 start = toInteger(start);
721 start = start < 0 ? max(length + start, 0) : min(start, length);
722
723 // support the de-facto SpiderMonkey extension
724 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice#Parameters
725 // https://bugs.ecmascript.org/show_bug.cgi?id=429
726 deleteCount = arguments.length == 1
727 ? length - start
728 : min(max(toInteger(deleteCount), 0), length - start);
729
730 return insert.call(object, start, deleteCount, slice.call(arguments, 2));
731 }
732
733 /**
734 * Converts the specified `value` to an integer.
735 *
736 * @private
737 * @param {Mixed} value The value to convert.
738 * @returns {Number} The resulting integer.
739 */
740 function toInteger(value) {
741 value = +value;
742 return value === 0 || !isFinite(value) ? value || 0 : value - (value % 1);
743 }
744
745 /**
746 * Appends arguments to the host array.
747 *
748 * @memberOf Benchmark.Suite
749 * @returns {Number} The new length.
750 */
751 function unshift() {
752 var object = Object(this);
753 insert.call(object, 0, 0, arguments);
754 return object.length;
755 }
756
757 /*--------------------------------------------------------------------------*/
758
759 /**
760 * A generic `Function#bind` like method.
761 *
762 * @private
763 * @param {Function} fn The function to be bound to `thisArg`.
764 * @param {Mixed} thisArg The `this` binding for the given function.
765 * @returns {Function} The bound function.
766 */
767 function bind(fn, thisArg) {
768 return function() { fn.apply(thisArg, arguments); };
769 }
770
771 /**
772 * Creates a function from the given arguments string and body.
773 *
774 * @private
775 * @param {String} args The comma separated function arguments.
776 * @param {String} body The function body.
777 * @returns {Function} The new function.
778 */
779 function createFunction() {
780 // lazy define
781 createFunction = function(args, body) {
782 var result,
783 anchor = freeDefine ? define.amd : Benchmark,
784 prop = uid + 'createFunction';
785
786 runScript((freeDefine ? 'define.amd.' : 'Benchmark.') + prop + '=function(' + args + '){' + body + '}');
787 result = anchor[prop];
788 delete anchor[prop];
789 return result;
790 };
791 // fix JaegerMonkey bug
792 // http://bugzil.la/639720
793 createFunction = support.browser && (createFunction('', 'return"' + uid + '"') || noop)() == uid ? createFunction : Function;
794 return createFunction.apply(null, arguments);
795 }
796
797 /**
798 * Delay the execution of a function based on the benchmark's `delay` property.
799 *
800 * @private
801 * @param {Object} bench The benchmark instance.
802 * @param {Object} fn The function to execute.
803 */
804 function delay(bench, fn) {
805 bench._timerId = setTimeout(fn, bench.delay * 1e3);
806 }
807
808 /**
809 * Destroys the given element.
810 *
811 * @private
812 * @param {Element} element The element to destroy.
813 */
814 function destroyElement(element) {
815 trash.appendChild(element);
816 trash.innerHTML = '';
817 }
818
819 /**
820 * Iterates over an object's properties, executing the `callback` for each.
821 * Callbacks may terminate the loop by explicitly returning `false`.
822 *
823 * @private
824 * @param {Object} object The object to iterate over.
825 * @param {Function} callback The function executed per own property.
826 * @param {Object} options The options object.
827 * @returns {Object} Returns the object iterated over.
828 */
829 function forProps() {
830 var forShadowed,
831 skipSeen,
832 forArgs = true,
833 shadowed = ['constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf'];
834
835 (function(enumFlag, key) {
836 // must use a non-native constructor to catch the Safari 2 issue
837 function Klass() { this.valueOf = 0; };
838 Klass.prototype.valueOf = 0;
839 // check various for-in bugs
840 for (key in new Klass) {
841 enumFlag += key == 'valueOf' ? 1 : 0;
842 }
843 // check if `arguments` objects have non-enumerable indexes
844 for (key in arguments) {
845 key == '0' && (forArgs = false);
846 }
847 // Safari 2 iterates over shadowed properties twice
848 // http://replay.waybackmachine.org/20090428222941/http://tobielangel.com/2007/1/29/for-in-loop-broken-in-safari/
849 skipSeen = enumFlag == 2;
850 // IE < 9 incorrectly makes an object's properties non-enumerable if they have
851 // the same name as other non-enumerable properties in its prototype chain.
852 forShadowed = !enumFlag;
853 }(0));
854
855 // lazy define
856 forProps = function(object, callback, options) {
857 options || (options = {});
858
859 var result = object;
860 object = Object(object);
861
862 var ctor,
863 key,
864 keys,
865 skipCtor,
866 done = !result,
867 which = options.which,
868 allFlag = which == 'all',
869 index = -1,
870 iteratee = object,
871 length = object.length,
872 ownFlag = allFlag || which == 'own',
873 seen = {},
874 skipProto = isClassOf(object, 'Function'),
875 thisArg = options.bind;
876
877 if (thisArg !== undefined) {
878 callback = bind(callback, thisArg);
879 }
880 // iterate all properties
881 if (allFlag && support.getAllKeys) {
882 for (index = 0, keys = getAllKeys(object), length = keys.length; index < length; index++) {
883 key = keys[index];
884 if (callback(object[key], key, object) === false) {
885 break;
886 }
887 }
888 }
889 // else iterate only enumerable properties
890 else {
891 for (key in object) {
892 // Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1
893 // (if the prototype or a property on the prototype has been set)
894 // incorrectly set a function's `prototype` property [[Enumerable]] value
895 // to `true`. Because of this we standardize on skipping the `prototype`
896 // property of functions regardless of their [[Enumerable]] value.
897 if ((done =
898 !(skipProto && key == 'prototype') &&
899 !(skipSeen && (hasKey(seen, key) || !(seen[key] = true))) &&
900 (!ownFlag || ownFlag && hasKey(object, key)) &&
901 callback(object[key], key, object) === false)) {
902 break;
903 }
904 }
905 // in IE < 9 strings don't support accessing characters by index
906 if (!done && (forArgs && isArguments(object) ||
907 ((noCharByIndex || noCharByOwnIndex) && isClassOf(object, 'String') &&
908 (iteratee = noCharByIndex ? object.split('') : object)))) {
909 while (++index < length) {
910 if ((done =
911 callback(iteratee[index], String(index), object) === false)) {
912 break;
913 }
914 }
915 }
916 if (!done && forShadowed) {
917 // Because IE < 9 can't set the `[[Enumerable]]` attribute of an existing
918 // property and the `constructor` property of a prototype defaults to
919 // non-enumerable, we manually skip the `constructor` property when we
920 // think we are iterating over a `prototype` object.
921 ctor = object.constructor;
922 skipCtor = ctor && ctor.prototype && ctor.prototype.constructor === ctor;
923 for (index = 0; index < 7; index++) {
924 key = shadowed[index];
925 if (!(skipCtor && key == 'constructor') &&
926 hasKey(object, key) &&
927 callback(object[key], key, object) === false) {
928 break;
929 }
930 }
931 }
932 }
933 return result;
934 };
935 return forProps.apply(null, arguments);
936 }
937
938 /**
939 * Gets the name of the first argument from a function's source.
940 *
941 * @private
942 * @param {Function} fn The function.
943 * @returns {String} The argument name.
944 */
945 function getFirstArgument(fn) {
946 return (!hasKey(fn, 'toString') &&
947 (/^[\s(]*function[^(]*\(([^\s,)]+)/.exec(fn) || 0)[1]) || '';
948 }
949
950 /**
951 * Computes the arithmetic mean of a sample.
952 *
953 * @private
954 * @param {Array} sample The sample.
955 * @returns {Number} The mean.
956 */
957 function getMean(sample) {
958 return reduce(sample, function(sum, x) {
959 return sum + x;
960 }) / sample.length || 0;
961 }
962
963 /**
964 * Gets the source code of a function.
965 *
966 * @private
967 * @param {Function} fn The function.
968 * @param {String} altSource A string used when a function's source code is unretrievable.
969 * @returns {String} The function's source code.
970 */
971 function getSource(fn, altSource) {
972 var result = altSource;
973 if (isStringable(fn)) {
974 result = String(fn);
975 } else if (support.decompilation) {
976 // escape the `{` for Firefox 1
977 result = (/^[^{]+\{([\s\S]*)}\s*$/.exec(fn) || 0)[1];
978 }
979 // trim string
980 result = (result || '').replace(/^\s+|\s+$/g, '');
981
982 // detect strings containing only the "use strict" directive
983 return /^(?:\/\*+[\w|\W]*?\*\/|\/\/.*?[\n\r\u2028\u2029]|\s)*(["'])use strict\1;?$/.test(result)
984 ? ''
985 : result;
986 }
987
988 /**
989 * Checks if a value is an `arguments` object.
990 *
991 * @private
992 * @param {Mixed} value The value to check.
993 * @returns {Boolean} Returns `true` if the value is an `arguments` object, else `false`.
994 */
995 function isArguments() {
996 // lazy define
997 isArguments = function(value) {
998 return toString.call(value) == '[object Arguments]';
999 };
1000 if (noArgumentsClass) {
1001 isArguments = function(value) {
1002 return hasKey(value, 'callee') &&
1003 !(propertyIsEnumerable && propertyIsEnumerable.call(value, 'callee'));
1004 };
1005 }
1006 return isArguments(arguments[0]);
1007 }
1008
1009 /**
1010 * Checks if an object is of the specified class.
1011 *
1012 * @private
1013 * @param {Mixed} value The value to check.
1014 * @param {String} name The name of the class.
1015 * @returns {Boolean} Returns `true` if the value is of the specified class, else `false`.
1016 */
1017 function isClassOf(value, name) {
1018 return value != null && toString.call(value) == '[object ' + name + ']';
1019 }
1020
1021 /**
1022 * Host objects can return type values that are different from their actual
1023 * data type. The objects we are concerned with usually return non-primitive
1024 * types of object, function, or unknown.
1025 *
1026 * @private
1027 * @param {Mixed} object The owner of the property.
1028 * @param {String} property The property to check.
1029 * @returns {Boolean} Returns `true` if the property value is a non-primitive, else `false`.
1030 */
1031 function isHostType(object, property) {
1032 var type = object != null ? typeof object[property] : 'number';
1033 return !/^(?:boolean|number|string|undefined)$/.test(type) &&
1034 (type == 'object' ? !!object[property] : true);
1035 }
1036
1037 /**
1038 * Checks if a given `value` is an object created by the `Object` constructor
1039 * assuming objects created by the `Object` constructor have no inherited
1040 * enumerable properties and that there are no `Object.prototype` extensions.
1041 *
1042 * @private
1043 * @param {Mixed} value The value to check.
1044 * @returns {Boolean} Returns `true` if the `value` is a plain `Object` object, else `false`.
1045 */
1046 function isPlainObject(value) {
1047 // avoid non-objects and false positives for `arguments` objects in IE < 9
1048 var result = false;
1049 if (!(value && typeof value == 'object') || (noArgumentsClass && isArguments(value))) {
1050 return result;
1051 }
1052 // IE < 9 presents DOM nodes as `Object` objects except they have `toString`
1053 // methods that are `typeof` "string" and still can coerce nodes to strings.
1054 // Also check that the constructor is `Object` (i.e. `Object instanceof Object`)
1055 var ctor = value.constructor;
1056 if ((support.nodeClass || !(typeof value.toString != 'function' && typeof (value + '') == 'string')) &&
1057 (!isClassOf(ctor, 'Function') || ctor instanceof ctor)) {
1058 // In most environments an object's own properties are iterated before
1059 // its inherited properties. If the last iterated property is an object's
1060 // own property then there are no inherited enumerable properties.
1061 if (support.iteratesOwnFirst) {
1062 forProps(value, function(subValue, subKey) {
1063 result = subKey;
1064 });
1065 return result === false || hasKey(value, result);
1066 }
1067 // IE < 9 iterates inherited properties before own properties. If the first
1068 // iterated property is an object's own property then there are no inherited
1069 // enumerable properties.
1070 forProps(value, function(subValue, subKey) {
1071 result = !hasKey(value, subKey);
1072 return false;
1073 });
1074 return result === false;
1075 }
1076 return result;
1077 }
1078
1079 /**
1080 * Checks if a value can be safely coerced to a string.
1081 *
1082 * @private
1083 * @param {Mixed} value The value to check.
1084 * @returns {Boolean} Returns `true` if the value can be coerced, else `false`.
1085 */
1086 function isStringable(value) {
1087 return hasKey(value, 'toString') || isClassOf(value, 'String');
1088 }
1089
1090 /**
1091 * Wraps a function and passes `this` to the original function as the
1092 * first argument.
1093 *
1094 * @private
1095 * @param {Function} fn The function to be wrapped.
1096 * @returns {Function} The new function.
1097 */
1098 function methodize(fn) {
1099 return function() {
1100 var args = [this];
1101 args.push.apply(args, arguments);
1102 return fn.apply(null, args);
1103 };
1104 }
1105
1106 /**
1107 * A no-operation function.
1108 *
1109 * @private
1110 */
1111 function noop() {
1112 // no operation performed
1113 }
1114
1115 /**
1116 * A wrapper around require() to suppress `module missing` errors.
1117 *
1118 * @private
1119 * @param {String} id The module id.
1120 * @returns {Mixed} The exported module or `null`.
1121 */
1122 function req(id) {
1123 try {
1124 var result = freeExports && freeRequire(id);
1125 } catch(e) { }
1126 return result || null;
1127 }
1128
1129 /**
1130 * Runs a snippet of JavaScript via script injection.
1131 *
1132 * @private
1133 * @param {String} code The code to run.
1134 */
1135 function runScript(code) {
1136 var anchor = freeDefine ? define.amd : Benchmark,
1137 script = doc.createElement('script'),
1138 sibling = doc.getElementsByTagName('script')[0],
1139 parent = sibling.parentNode,
1140 prop = uid + 'runScript',
1141 prefix = '(' + (freeDefine ? 'define.amd.' : 'Benchmark.') + prop + '||function(){})();';
1142
1143 // Firefox 2.0.0.2 cannot use script injection as intended because it executes
1144 // asynchronously, but that's OK because script injection is only used to avoid
1145 // the previously commented JaegerMonkey bug.
1146 try {
1147 // remove the inserted script *before* running the code to avoid differences
1148 // in the expected script element count/order of the document.
1149 script.appendChild(doc.createTextNode(prefix + code));
1150 anchor[prop] = function() { destroyElement(script); };
1151 } catch(e) {
1152 parent = parent.cloneNode(false);
1153 sibling = null;
1154 script.text = code;
1155 }
1156 parent.insertBefore(script, sibling);
1157 delete anchor[prop];
1158 }
1159
1160 /**
1161 * A helper function for setting options/event handlers.
1162 *
1163 * @private
1164 * @param {Object} bench The benchmark instance.
1165 * @param {Object} [options={}] Options object.
1166 */
1167 function setOptions(bench, options) {
1168 options = extend({}, bench.constructor.options, options);
1169 bench.options = forOwn(options, function(value, key) {
1170 if (value != null) {
1171 // add event listeners
1172 if (/^on[A-Z]/.test(key)) {
1173 forEach(key.split(' '), function(key) {
1174 bench.on(key.slice(2).toLowerCase(), value);
1175 });
1176 } else if (!hasKey(bench, key)) {
1177 bench[key] = deepClone(value);
1178 }
1179 }
1180 });
1181 }
1182
1183 /*--------------------------------------------------------------------------*/
1184
1185 /**
1186 * Handles cycling/completing the deferred benchmark.
1187 *
1188 * @memberOf Benchmark.Deferred
1189 */
1190 function resolve() {
1191 var me = this,
1192 clone = me.benchmark,
1193 bench = clone._original;
1194
1195 if (bench.aborted) {
1196 // cycle() -> clone cycle/complete event -> compute()'s invoked bench.run() cycle/complete
1197 me.teardown();
1198 clone.running = false;
1199 cycle(me);
1200 }
1201 else if (++me.cycles < clone.count) {
1202 // continue the test loop
1203 if (support.timeout) {
1204 // use setTimeout to avoid a call stack overflow if called recursively
1205 setTimeout(function() { clone.compiled.call(me, timer); }, 0);
1206 } else {
1207 clone.compiled.call(me, timer);
1208 }
1209 }
1210 else {
1211 timer.stop(me);
1212 me.teardown();
1213 delay(clone, function() { cycle(me); });
1214 }
1215 }
1216
1217 /*--------------------------------------------------------------------------*/
1218
1219 /**
1220 * A deep clone utility.
1221 *
1222 * @static
1223 * @memberOf Benchmark
1224 * @param {Mixed} value The value to clone.
1225 * @returns {Mixed} The cloned value.
1226 */
1227 function deepClone(value) {
1228 var accessor,
1229 circular,
1230 clone,
1231 ctor,
1232 descriptor,
1233 extensible,
1234 key,
1235 length,
1236 markerKey,
1237 parent,
1238 result,
1239 source,
1240 subIndex,
1241 data = { 'value': value },
1242 index = 0,
1243 marked = [],
1244 queue = { 'length': 0 },
1245 unmarked = [];
1246
1247 /**
1248 * An easily detectable decorator for cloned values.
1249 */
1250 function Marker(object) {
1251 this.raw = object;
1252 }
1253
1254 /**
1255 * The callback used by `forProps()`.
1256 */
1257 function forPropsCallback(subValue, subKey) {
1258 // exit early to avoid cloning the marker
1259 if (subValue && subValue.constructor == Marker) {
1260 return;
1261 }
1262 // add objects to the queue
1263 if (subValue === Object(subValue)) {
1264 queue[queue.length++] = { 'key': subKey, 'parent': clone, 'source': value };
1265 }
1266 // assign non-objects
1267 else {
1268 try {
1269 // will throw an error in strict mode if the property is read-only
1270 clone[subKey] = subValue;
1271 } catch(e) { }
1272 }
1273 }
1274
1275 /**
1276 * Gets an available marker key for the given object.
1277 */
1278 function getMarkerKey(object) {
1279 // avoid collisions with existing keys
1280 var result = uid;
1281 while (object[result] && object[result].constructor != Marker) {
1282 result += 1;
1283 }
1284 return result;
1285 }
1286
1287 do {
1288 key = data.key;
1289 parent = data.parent;
1290 source = data.source;
1291 clone = value = source ? source[key] : data.value;
1292 accessor = circular = descriptor = false;
1293
1294 // create a basic clone to filter out functions, DOM elements, and
1295 // other non `Object` objects
1296 if (value === Object(value)) {
1297 // use custom deep clone function if available
1298 if (isClassOf(value.deepClone, 'Function')) {
1299 clone = value.deepClone();
1300 } else {
1301 ctor = value.constructor;
1302 switch (toString.call(value)) {
1303 case '[object Array]':
1304 clone = new ctor(value.length);
1305 break;
1306
1307 case '[object Boolean]':
1308 clone = new ctor(value == true);
1309 break;
1310
1311 case '[object Date]':
1312 clone = new ctor(+value);
1313 break;
1314
1315 case '[object Object]':
1316 isPlainObject(value) && (clone = {});
1317 break;
1318
1319 case '[object Number]':
1320 case '[object String]':
1321 clone = new ctor(value);
1322 break;
1323
1324 case '[object RegExp]':
1325 clone = ctor(value.source,
1326 (value.global ? 'g' : '') +
1327 (value.ignoreCase ? 'i' : '') +
1328 (value.multiline ? 'm' : ''));
1329 }
1330 }
1331 // continue clone if `value` doesn't have an accessor descriptor
1332 // http://es5.github.com/#x8.10.1
1333 if (clone && clone != value &&
1334 !(descriptor = source && support.descriptors && getDescriptor(source, key),
1335 accessor = descriptor && (descriptor.get || descriptor.set))) {
1336 // use an existing clone (circular reference)
1337 if ((extensible = isExtensible(value))) {
1338 markerKey = getMarkerKey(value);
1339 if (value[markerKey]) {
1340 circular = clone = value[markerKey].raw;
1341 }
1342 } else {
1343 // for frozen/sealed objects
1344 for (subIndex = 0, length = unmarked.length; subIndex < length; subIndex++) {
1345 data = unmarked[subIndex];
1346 if (data.object === value) {
1347 circular = clone = data.clone;
1348 break;
1349 }
1350 }
1351 }
1352 if (!circular) {
1353 // mark object to allow quickly detecting circular references and tie it to its clone
1354 if (extensible) {
1355 value[markerKey] = new Marker(clone);
1356 marked.push({ 'key': markerKey, 'object': value });
1357 } else {
1358 // for frozen/sealed objects
1359 unmarked.push({ 'clone': clone, 'object': value });
1360 }
1361 // iterate over object properties
1362 forProps(value, forPropsCallback, { 'which': 'all' });
1363 }
1364 }
1365 }
1366 if (parent) {
1367 // for custom property descriptors
1368 if (accessor || (descriptor && !(descriptor.configurable && descriptor.enumerable && descriptor.writable))) {
1369 if ('value' in descriptor) {
1370 descriptor.value = clone;
1371 }
1372 setDescriptor(parent, key, descriptor);
1373 }
1374 // for default property descriptors
1375 else {
1376 parent[key] = clone;
1377 }
1378 } else {
1379 result = clone;
1380 }
1381 } while ((data = queue[index++]));
1382
1383 // remove markers
1384 for (index = 0, length = marked.length; index < length; index++) {
1385 data = marked[index];
1386 delete data.object[data.key];
1387 }
1388 return result;
1389 }
1390
1391 /**
1392 * An iteration utility for arrays and objects.
1393 * Callbacks may terminate the loop by explicitly returning `false`.
1394 *
1395 * @static
1396 * @memberOf Benchmark
1397 * @param {Array|Object} object The object to iterate over.
1398 * @param {Function} callback The function called per iteration.
1399 * @param {Mixed} thisArg The `this` binding for the callback.
1400 * @returns {Array|Object} Returns the object iterated over.
1401 */
1402 function each(object, callback, thisArg) {
1403 var result = object;
1404 object = Object(object);
1405
1406 var fn = callback,
1407 index = -1,
1408 length = object.length,
1409 isSnapshot = !!(object.snapshotItem && (length = object.snapshotLength)),
1410 isSplittable = (noCharByIndex || noCharByOwnIndex) && isClassOf(object, 'String'),
1411 isConvertable = isSnapshot || isSplittable || 'item' in object,
1412 origObject = object;
1413
1414 // in Opera < 10.5 `hasKey(object, 'length')` returns `false` for NodeLists
1415 if (length === length >>> 0) {
1416 if (isConvertable) {
1417 // the third argument of the callback is the original non-array object
1418 callback = function(value, index) {
1419 return fn.call(this, value, index, origObject);
1420 };
1421 // in IE < 9 strings don't support accessing characters by index
1422 if (isSplittable) {
1423 object = object.split('');
1424 } else {
1425 object = [];
1426 while (++index < length) {
1427 // in Safari 2 `index in object` is always `false` for NodeLists
1428 object[index] = isSnapshot ? result.snapshotItem(index) : result[index];
1429 }
1430 }
1431 }
1432 forEach(object, callback, thisArg);
1433 } else {
1434 forOwn(object, callback, thisArg);
1435 }
1436 return result;
1437 }
1438
1439 /**
1440 * Copies enumerable properties from the source(s) object to the destination object.
1441 *
1442 * @static
1443 * @memberOf Benchmark
1444 * @param {Object} destination The destination object.
1445 * @param {Object} [source={}] The source object.
1446 * @returns {Object} The destination object.
1447 */
1448 function extend(destination, source) {
1449 // Chrome < 14 incorrectly sets `destination` to `undefined` when we `delete arguments[0]`
1450 // http://code.google.com/p/v8/issues/detail?id=839
1451 var result = destination;
1452 delete arguments[0];
1453
1454 forEach(arguments, function(source) {
1455 forProps(source, function(value, key) {
1456 result[key] = value;
1457 });
1458 });
1459 return result;
1460 }
1461
1462 /**
1463 * A generic `Array#filter` like method.
1464 *
1465 * @static
1466 * @memberOf Benchmark
1467 * @param {Array} array The array to iterate over.
1468 * @param {Function|String} callback The function/alias called per iteration.
1469 * @param {Mixed} thisArg The `this` binding for the callback.
1470 * @returns {Array} A new array of values that passed callback filter.
1471 * @example
1472 *
1473 * // get odd numbers
1474 * Benchmark.filter([1, 2, 3, 4, 5], function(n) {
1475 * return n % 2;
1476 * }); // -> [1, 3, 5];
1477 *
1478 * // get fastest benchmarks
1479 * Benchmark.filter(benches, 'fastest');
1480 *
1481 * // get slowest benchmarks
1482 * Benchmark.filter(benches, 'slowest');
1483 *
1484 * // get benchmarks that completed without erroring
1485 * Benchmark.filter(benches, 'successful');
1486 */
1487 function filter(array, callback, thisArg) {
1488 var result;
1489
1490 if (callback == 'successful') {
1491 // callback to exclude those that are errored, unrun, or have hz of Infinity
1492 callback = function(bench) { return bench.cycles && isFinite(bench.hz); };
1493 }
1494 else if (callback == 'fastest' || callback == 'slowest') {
1495 // get successful, sort by period + margin of error, and filter fastest/slowest
1496 result = filter(array, 'successful').sort(function(a, b) {
1497 a = a.stats; b = b.stats;
1498 return (a.mean + a.moe > b.mean + b.moe ? 1 : -1) * (callback == 'fastest' ? 1 : -1);
1499 });
1500 result = filter(result, function(bench) {
1501 return result[0].compare(bench) == 0;
1502 });
1503 }
1504 return result || reduce(array, function(result, value, index) {
1505 return callback.call(thisArg, value, index, array) ? (result.push(value), result) : result;
1506 }, []);
1507 }
1508
1509 /**
1510 * A generic `Array#forEach` like method.
1511 * Callbacks may terminate the loop by explicitly returning `false`.
1512 *
1513 * @static
1514 * @memberOf Benchmark
1515 * @param {Array} array The array to iterate over.
1516 * @param {Function} callback The function called per iteration.
1517 * @param {Mixed} thisArg The `this` binding for the callback.
1518 * @returns {Array} Returns the array iterated over.
1519 */
1520 function forEach(array, callback, thisArg) {
1521 var index = -1,
1522 length = (array = Object(array)).length >>> 0;
1523
1524 if (thisArg !== undefined) {
1525 callback = bind(callback, thisArg);
1526 }
1527 while (++index < length) {
1528 if (index in array &&
1529 callback(array[index], index, array) === false) {
1530 break;
1531 }
1532 }
1533 return array;
1534 }
1535
1536 /**
1537 * Iterates over an object's own properties, executing the `callback` for each.
1538 * Callbacks may terminate the loop by explicitly returning `false`.
1539 *
1540 * @static
1541 * @memberOf Benchmark
1542 * @param {Object} object The object to iterate over.
1543 * @param {Function} callback The function executed per own property.
1544 * @param {Mixed} thisArg The `this` binding for the callback.
1545 * @returns {Object} Returns the object iterated over.
1546 */
1547 function forOwn(object, callback, thisArg) {
1548 return forProps(object, callback, { 'bind': thisArg, 'which': 'own' });
1549 }
1550
1551 /**
1552 * Converts a number to a more readable comma-separated string representation.
1553 *
1554 * @static
1555 * @memberOf Benchmark
1556 * @param {Number} number The number to convert.
1557 * @returns {String} The more readable string representation.
1558 */
1559 function formatNumber(number) {
1560 number = String(number).split('.');
1561 return number[0].replace(/(?=(?:\d{3})+$)(?!\b)/g, ',') +
1562 (number[1] ? '.' + number[1] : '');
1563 }
1564
1565 /**
1566 * Checks if an object has the specified key as a direct property.
1567 *
1568 * @static
1569 * @memberOf Benchmark
1570 * @param {Object} object The object to check.
1571 * @param {String} key The key to check for.
1572 * @returns {Boolean} Returns `true` if key is a direct property, else `false`.
1573 */
1574 function hasKey() {
1575 // lazy define for worst case fallback (not as accurate)
1576 hasKey = function(object, key) {
1577 var parent = object != null && (object.constructor || Object).prototype;
1578 return !!parent && key in Object(object) && !(key in parent && object[key] === parent[key]);
1579 };
1580 // for modern browsers
1581 if (isClassOf(hasOwnProperty, 'Function')) {
1582 hasKey = function(object, key) {
1583 return object != null && hasOwnProperty.call(object, key);
1584 };
1585 }
1586 // for Safari 2
1587 else if ({}.__proto__ == Object.prototype) {
1588 hasKey = function(object, key) {
1589 var result = false;
1590 if (object != null) {
1591 object = Object(object);
1592 object.__proto__ = [object.__proto__, object.__proto__ = null, result = key in object][0];
1593 }
1594 return result;
1595 };
1596 }
1597 return hasKey.apply(this, arguments);
1598 }
1599
1600 /**
1601 * A generic `Array#indexOf` like method.
1602 *
1603 * @static
1604 * @memberOf Benchmark
1605 * @param {Array} array The array to iterate over.
1606 * @param {Mixed} value The value to search for.
1607 * @param {Number} [fromIndex=0] The index to start searching from.
1608 * @returns {Number} The index of the matched value or `-1`.
1609 */
1610 function indexOf(array, value, fromIndex) {
1611 var index = toInteger(fromIndex),
1612 length = (array = Object(array)).length >>> 0;
1613
1614 index = (index < 0 ? max(0, length + index) : index) - 1;
1615 while (++index < length) {
1616 if (index in array && value === array[index]) {
1617 return index;
1618 }
1619 }
1620 return -1;
1621 }
1622
1623 /**
1624 * Modify a string by replacing named tokens with matching object property values.
1625 *
1626 * @static
1627 * @memberOf Benchmark
1628 * @param {String} string The string to modify.
1629 * @param {Object} object The template object.
1630 * @returns {String} The modified string.
1631 */
1632 function interpolate(string, object) {
1633 forOwn(object, function(value, key) {
1634 // escape regexp special characters in `key`
1635 string = string.replace(RegExp('#\\{' + key.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1') + '\\}', 'g'), value);
1636 });
1637 return string;
1638 }
1639
1640 /**
1641 * Invokes a method on all items in an array.
1642 *
1643 * @static
1644 * @memberOf Benchmark
1645 * @param {Array} benches Array of benchmarks to iterate over.
1646 * @param {String|Object} name The name of the method to invoke OR options object.
1647 * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the method with.
1648 * @returns {Array} A new array of values returned from each method invoked.
1649 * @example
1650 *
1651 * // invoke `reset` on all benchmarks
1652 * Benchmark.invoke(benches, 'reset');
1653 *
1654 * // invoke `emit` with arguments
1655 * Benchmark.invoke(benches, 'emit', 'complete', listener);
1656 *
1657 * // invoke `run(true)`, treat benchmarks as a queue, and register invoke callbacks
1658 * Benchmark.invoke(benches, {
1659 *
1660 * // invoke the `run` method
1661 * 'name': 'run',
1662 *
1663 * // pass a single argument
1664 * 'args': true,
1665 *
1666 * // treat as queue, removing benchmarks from front of `benches` until empty
1667 * 'queued': true,
1668 *
1669 * // called before any benchmarks have been invoked.
1670 * 'onStart': onStart,
1671 *
1672 * // called between invoking benchmarks
1673 * 'onCycle': onCycle,
1674 *
1675 * // called after all benchmarks have been invoked.
1676 * 'onComplete': onComplete
1677 * });
1678 */
1679 function invoke(benches, name) {
1680 var args,
1681 bench,
1682 queued,
1683 index = -1,
1684 eventProps = { 'currentTarget': benches },
1685 options = { 'onStart': noop, 'onCycle': noop, 'onComplete': noop },
1686 result = map(benches, function(bench) { return bench; });
1687
1688 /**
1689 * Invokes the method of the current object and if synchronous, fetches the next.
1690 */
1691 function execute() {
1692 var listeners,
1693 async = isAsync(bench);
1694
1695 if (async) {
1696 // use `getNext` as the first listener
1697 bench.on('complete', getNext);
1698 listeners = bench.events.complete;
1699 listeners.splice(0, 0, listeners.pop());
1700 }
1701 // execute method
1702 result[index] = isClassOf(bench && bench[name], 'Function') ? bench[name].apply(bench, args) : undefined;
1703 // if synchronous return true until finished
1704 return !async && getNext();
1705 }
1706
1707 /**
1708 * Fetches the next bench or executes `onComplete` callback.
1709 */
1710 function getNext(event) {
1711 var cycleEvent,
1712 last = bench,
1713 async = isAsync(last);
1714
1715 if (async) {
1716 last.off('complete', getNext);
1717 last.emit('complete');
1718 }
1719 // emit "cycle" event
1720 eventProps.type = 'cycle';
1721 eventProps.target = last;
1722 cycleEvent = Event(eventProps);
1723 options.onCycle.call(benches, cycleEvent);
1724
1725 // choose next benchmark if not exiting early
1726 if (!cycleEvent.aborted && raiseIndex() !== false) {
1727 bench = queued ? benches[0] : result[index];
1728 if (isAsync(bench)) {
1729 delay(bench, execute);
1730 }
1731 else if (async) {
1732 // resume execution if previously asynchronous but now synchronous
1733 while (execute()) { }
1734 }
1735 else {
1736 // continue synchronous execution
1737 return true;
1738 }
1739 } else {
1740 // emit "complete" event
1741 eventProps.type = 'complete';
1742 options.onComplete.call(benches, Event(eventProps));
1743 }
1744 // When used as a listener `event.aborted = true` will cancel the rest of
1745 // the "complete" listeners because they were already called above and when
1746 // used as part of `getNext` the `return false` will exit the execution while-loop.
1747 if (event) {
1748 event.aborted = true;
1749 } else {
1750 return false;
1751 }
1752 }
1753
1754 /**
1755 * Checks if invoking `Benchmark#run` with asynchronous cycles.
1756 */
1757 function isAsync(object) {
1758 // avoid using `instanceof` here because of IE memory leak issues with host objects
1759 var async = args[0] && args[0].async;
1760 return Object(object).constructor == Benchmark && name == 'run' &&
1761 ((async == null ? object.options.async : async) && support.timeout || object.defer);
1762 }
1763
1764 /**
1765 * Raises `index` to the next defined index or returns `false`.
1766 */
1767 function raiseIndex() {
1768 var length = result.length;
1769 if (queued) {
1770 // if queued remove the previous bench and subsequent skipped non-entries
1771 do {
1772 ++index > 0 && shift.call(benches);
1773 } while ((length = benches.length) && !('0' in benches));
1774 }
1775 else {
1776 while (++index < length && !(index in result)) { }
1777 }
1778 // if we reached the last index then return `false`
1779 return (queued ? length : index < length) ? index : (index = false);
1780 }
1781
1782 // juggle arguments
1783 if (isClassOf(name, 'String')) {
1784 // 2 arguments (array, name)
1785 args = slice.call(arguments, 2);
1786 } else {
1787 // 2 arguments (array, options)
1788 options = extend(options, name);
1789 name = options.name;
1790 args = isClassOf(args = 'args' in options ? options.args : [], 'Array') ? args : [args];
1791 queued = options.queued;
1792 }
1793
1794 // start iterating over the array
1795 if (raiseIndex() !== false) {
1796 // emit "start" event
1797 bench = result[index];
1798 eventProps.type = 'start';
1799 eventProps.target = bench;
1800 options.onStart.call(benches, Event(eventProps));
1801
1802 // end early if the suite was aborted in an "onStart" listener
1803 if (benches.aborted && benches.constructor == Suite && name == 'run') {
1804 // emit "cycle" event
1805 eventProps.type = 'cycle';
1806 options.onCycle.call(benches, Event(eventProps));
1807 // emit "complete" event
1808 eventProps.type = 'complete';
1809 options.onComplete.call(benches, Event(eventProps));
1810 }
1811 // else start
1812 else {
1813 if (isAsync(bench)) {
1814 delay(bench, execute);
1815 } else {
1816 while (execute()) { }
1817 }
1818 }
1819 }
1820 return result;
1821 }
1822
1823 /**
1824 * Creates a string of joined array values or object key-value pairs.
1825 *
1826 * @static
1827 * @memberOf Benchmark
1828 * @param {Array|Object} object The object to operate on.
1829 * @param {String} [separator1=','] The separator used between key-value pairs.
1830 * @param {String} [separator2=': '] The separator used between keys and values.
1831 * @returns {String} The joined result.
1832 */
1833 function join(object, separator1, separator2) {
1834 var result = [],
1835 length = (object = Object(object)).length,
1836 arrayLike = length === length >>> 0;
1837
1838 separator2 || (separator2 = ': ');
1839 each(object, function(value, key) {
1840 result.push(arrayLike ? value : key + separator2 + value);
1841 });
1842 return result.join(separator1 || ',');
1843 }
1844
1845 /**
1846 * A generic `Array#map` like method.
1847 *
1848 * @static
1849 * @memberOf Benchmark
1850 * @param {Array} array The array to iterate over.
1851 * @param {Function} callback The function called per iteration.
1852 * @param {Mixed} thisArg The `this` binding for the callback.
1853 * @returns {Array} A new array of values returned by the callback.
1854 */
1855 function map(array, callback, thisArg) {
1856 return reduce(array, function(result, value, index) {
1857 result[index] = callback.call(thisArg, value, index, array);
1858 return result;
1859 }, Array(Object(array).length >>> 0));
1860 }
1861
1862 /**
1863 * Retrieves the value of a specified property from all items in an array.
1864 *
1865 * @static
1866 * @memberOf Benchmark
1867 * @param {Array} array The array to iterate over.
1868 * @param {String} property The property to pluck.
1869 * @returns {Array} A new array of property values.
1870 */
1871 function pluck(array, property) {
1872 return map(array, function(object) {
1873 return object == null ? undefined : object[property];
1874 });
1875 }
1876
1877 /**
1878 * A generic `Array#reduce` like method.
1879 *
1880 * @static
1881 * @memberOf Benchmark
1882 * @param {Array} array The array to iterate over.
1883 * @param {Function} callback The function called per iteration.
1884 * @param {Mixed} accumulator Initial value of the accumulator.
1885 * @returns {Mixed} The accumulator.
1886 */
1887 function reduce(array, callback, accumulator) {
1888 var noaccum = arguments.length < 3;
1889 forEach(array, function(value, index) {
1890 accumulator = noaccum ? (noaccum = false, value) : callback(accumulator, value, index, array);
1891 });
1892 return accumulator;
1893 }
1894
1895 /*--------------------------------------------------------------------------*/
1896
1897 /**
1898 * Aborts all benchmarks in the suite.
1899 *
1900 * @name abort
1901 * @memberOf Benchmark.Suite
1902 * @returns {Object} The suite instance.
1903 */
1904 function abortSuite() {
1905 var event,
1906 me = this,
1907 resetting = calledBy.resetSuite;
1908
1909 if (me.running) {
1910 event = Event('abort');
1911 me.emit(event);
1912 if (!event.cancelled || resetting) {
1913 // avoid infinite recursion
1914 calledBy.abortSuite = true;
1915 me.reset();
1916 delete calledBy.abortSuite;
1917
1918 if (!resetting) {
1919 me.aborted = true;
1920 invoke(me, 'abort');
1921 }
1922 }
1923 }
1924 return me;
1925 }
1926
1927 /**
1928 * Adds a test to the benchmark suite.
1929 *
1930 * @memberOf Benchmark.Suite
1931 * @param {String} name A name to identify the benchmark.
1932 * @param {Function|String} fn The test to benchmark.
1933 * @param {Object} [options={}] Options object.
1934 * @returns {Object} The benchmark instance.
1935 * @example
1936 *
1937 * // basic usage
1938 * suite.add(fn);
1939 *
1940 * // or using a name first
1941 * suite.add('foo', fn);
1942 *
1943 * // or with options
1944 * suite.add('foo', fn, {
1945 * 'onCycle': onCycle,
1946 * 'onComplete': onComplete
1947 * });
1948 *
1949 * // or name and options
1950 * suite.add('foo', {
1951 * 'fn': fn,
1952 * 'onCycle': onCycle,
1953 * 'onComplete': onComplete
1954 * });
1955 *
1956 * // or options only
1957 * suite.add({
1958 * 'name': 'foo',
1959 * 'fn': fn,
1960 * 'onCycle': onCycle,
1961 * 'onComplete': onComplete
1962 * });
1963 */
1964 function add(name, fn, options) {
1965 var me = this,
1966 bench = Benchmark(name, fn, options),
1967 event = Event({ 'type': 'add', 'target': bench });
1968
1969 if (me.emit(event), !event.cancelled) {
1970 me.push(bench);
1971 }
1972 return me;
1973 }
1974
1975 /**
1976 * Creates a new suite with cloned benchmarks.
1977 *
1978 * @name clone
1979 * @memberOf Benchmark.Suite
1980 * @param {Object} options Options object to overwrite cloned options.
1981 * @returns {Object} The new suite instance.
1982 */
1983 function cloneSuite(options) {
1984 var me = this,
1985 result = new me.constructor(extend({}, me.options, options));
1986
1987 // copy own properties
1988 forOwn(me, function(value, key) {
1989 if (!hasKey(result, key)) {
1990 result[key] = value && isClassOf(value.clone, 'Function')
1991 ? value.clone()
1992 : deepClone(value);
1993 }
1994 });
1995 return result;
1996 }
1997
1998 /**
1999 * An `Array#filter` like method.
2000 *
2001 * @name filter
2002 * @memberOf Benchmark.Suite
2003 * @param {Function|String} callback The function/alias called per iteration.
2004 * @returns {Object} A new suite of benchmarks that passed callback filter.
2005 */
2006 function filterSuite(callback) {
2007 var me = this,
2008 result = new me.constructor;
2009
2010 result.push.apply(result, filter(me, callback));
2011 return result;
2012 }
2013
2014 /**
2015 * Resets all benchmarks in the suite.
2016 *
2017 * @name reset
2018 * @memberOf Benchmark.Suite
2019 * @returns {Object} The suite instance.
2020 */
2021 function resetSuite() {
2022 var event,
2023 me = this,
2024 aborting = calledBy.abortSuite;
2025
2026 if (me.running && !aborting) {
2027 // no worries, `resetSuite()` is called within `abortSuite()`
2028 calledBy.resetSuite = true;
2029 me.abort();
2030 delete calledBy.resetSuite;
2031 }
2032 // reset if the state has changed
2033 else if ((me.aborted || me.running) &&
2034 (me.emit(event = Event('reset')), !event.cancelled)) {
2035 me.running = false;
2036 if (!aborting) {
2037 invoke(me, 'reset');
2038 }
2039 }
2040 return me;
2041 }
2042
2043 /**
2044 * Runs the suite.
2045 *
2046 * @name run
2047 * @memberOf Benchmark.Suite
2048 * @param {Object} [options={}] Options object.
2049 * @returns {Object} The suite instance.
2050 * @example
2051 *
2052 * // basic usage
2053 * suite.run();
2054 *
2055 * // or with options
2056 * suite.run({ 'async': true, 'queued': true });
2057 */
2058 function runSuite(options) {
2059 var me = this;
2060
2061 me.reset();
2062 me.running = true;
2063 options || (options = {});
2064
2065 invoke(me, {
2066 'name': 'run',
2067 'args': options,
2068 'queued': options.queued,
2069 'onStart': function(event) {
2070 me.emit(event);
2071 },
2072 'onCycle': function(event) {
2073 var bench = event.target;
2074 if (bench.error) {
2075 me.emit({ 'type': 'error', 'target': bench });
2076 }
2077 me.emit(event);
2078 event.aborted = me.aborted;
2079 },
2080 'onComplete': function(event) {
2081 me.running = false;
2082 me.emit(event);
2083 }
2084 });
2085 return me;
2086 }
2087
2088 /*--------------------------------------------------------------------------*/
2089
2090 /**
2091 * Executes all registered listeners of the specified event type.
2092 *
2093 * @memberOf Benchmark, Benchmark.Suite
2094 * @param {String|Object} type The event type or object.
2095 * @returns {Mixed} Returns the return value of the last listener executed.
2096 */
2097 function emit(type) {
2098 var listeners,
2099 me = this,
2100 event = Event(type),
2101 events = me.events,
2102 args = (arguments[0] = event, arguments);
2103
2104 event.currentTarget || (event.currentTarget = me);
2105 event.target || (event.target = me);
2106 delete event.result;
2107
2108 if (events && (listeners = hasKey(events, event.type) && events[event.type])) {
2109 forEach(listeners.slice(), function(listener) {
2110 if ((event.result = listener.apply(me, args)) === false) {
2111 event.cancelled = true;
2112 }
2113 return !event.aborted;
2114 });
2115 }
2116 return event.result;
2117 }
2118
2119 /**
2120 * Returns an array of event listeners for a given type that can be manipulated
2121 * to add or remove listeners.
2122 *
2123 * @memberOf Benchmark, Benchmark.Suite
2124 * @param {String} type The event type.
2125 * @returns {Array} The listeners array.
2126 */
2127 function listeners(type) {
2128 var me = this,
2129 events = me.events || (me.events = {});
2130
2131 return hasKey(events, type) ? events[type] : (events[type] = []);
2132 }
2133
2134 /**
2135 * Unregisters a listener for the specified event type(s),
2136 * or unregisters all listeners for the specified event type(s),
2137 * or unregisters all listeners for all event types.
2138 *
2139 * @memberOf Benchmark, Benchmark.Suite
2140 * @param {String} [type] The event type.
2141 * @param {Function} [listener] The function to unregister.
2142 * @returns {Object} The benchmark instance.
2143 * @example
2144 *
2145 * // unregister a listener for an event type
2146 * bench.off('cycle', listener);
2147 *
2148 * // unregister a listener for multiple event types
2149 * bench.off('start cycle', listener);
2150 *
2151 * // unregister all listeners for an event type
2152 * bench.off('cycle');
2153 *
2154 * // unregister all listeners for multiple event types
2155 * bench.off('start cycle complete');
2156 *
2157 * // unregister all listeners for all event types
2158 * bench.off();
2159 */
2160 function off(type, listener) {
2161 var me = this,
2162 events = me.events;
2163
2164 events && each(type ? type.split(' ') : events, function(listeners, type) {
2165 var index;
2166 if (typeof listeners == 'string') {
2167 type = listeners;
2168 listeners = hasKey(events, type) && events[type];
2169 }
2170 if (listeners) {
2171 if (listener) {
2172 index = indexOf(listeners, listener);
2173 if (index > -1) {
2174 listeners.splice(index, 1);
2175 }
2176 } else {
2177 listeners.length = 0;
2178 }
2179 }
2180 });
2181 return me;
2182 }
2183
2184 /**
2185 * Registers a listener for the specified event type(s).
2186 *
2187 * @memberOf Benchmark, Benchmark.Suite
2188 * @param {String} type The event type.
2189 * @param {Function} listener The function to register.
2190 * @returns {Object} The benchmark instance.
2191 * @example
2192 *
2193 * // register a listener for an event type
2194 * bench.on('cycle', listener);
2195 *
2196 * // register a listener for multiple event types
2197 * bench.on('start cycle', listener);
2198 */
2199 function on(type, listener) {
2200 var me = this,
2201 events = me.events || (me.events = {});
2202
2203 forEach(type.split(' '), function(type) {
2204 (hasKey(events, type)
2205 ? events[type]
2206 : (events[type] = [])
2207 ).push(listener);
2208 });
2209 return me;
2210 }
2211
2212 /*--------------------------------------------------------------------------*/
2213
2214 /**
2215 * Aborts the benchmark without recording times.
2216 *
2217 * @memberOf Benchmark
2218 * @returns {Object} The benchmark instance.
2219 */
2220 function abort() {
2221 var event,
2222 me = this,
2223 resetting = calledBy.reset;
2224
2225 if (me.running) {
2226 event = Event('abort');
2227 me.emit(event);
2228 if (!event.cancelled || resetting) {
2229 // avoid infinite recursion
2230 calledBy.abort = true;
2231 me.reset();
2232 delete calledBy.abort;
2233
2234 if (support.timeout) {
2235 clearTimeout(me._timerId);
2236 delete me._timerId;
2237 }
2238 if (!resetting) {
2239 me.aborted = true;
2240 me.running = false;
2241 }
2242 }
2243 }
2244 return me;
2245 }
2246
2247 /**
2248 * Creates a new benchmark using the same test and options.
2249 *
2250 * @memberOf Benchmark
2251 * @param {Object} options Options object to overwrite cloned options.
2252 * @returns {Object} The new benchmark instance.
2253 * @example
2254 *
2255 * var bizarro = bench.clone({
2256 * 'name': 'doppelganger'
2257 * });
2258 */
2259 function clone(options) {
2260 var me = this,
2261 result = new me.constructor(extend({}, me, options));
2262
2263 // correct the `options` object
2264 result.options = extend({}, me.options, options);
2265
2266 // copy own custom properties
2267 forOwn(me, function(value, key) {
2268 if (!hasKey(result, key)) {
2269 result[key] = deepClone(value);
2270 }
2271 });
2272 return result;
2273 }
2274
2275 /**
2276 * Determines if a benchmark is faster than another.
2277 *
2278 * @memberOf Benchmark
2279 * @param {Object} other The benchmark to compare.
2280 * @returns {Number} Returns `-1` if slower, `1` if faster, and `0` if indeterminate.
2281 */
2282 function compare(other) {
2283 var critical,
2284 zStat,
2285 me = this,
2286 sample1 = me.stats.sample,
2287 sample2 = other.stats.sample,
2288 size1 = sample1.length,
2289 size2 = sample2.length,
2290 maxSize = max(size1, size2),
2291 minSize = min(size1, size2),
2292 u1 = getU(sample1, sample2),
2293 u2 = getU(sample2, sample1),
2294 u = min(u1, u2);
2295
2296 function getScore(xA, sampleB) {
2297 return reduce(sampleB, function(total, xB) {
2298 return total + (xB > xA ? 0 : xB < xA ? 1 : 0.5);
2299 }, 0);
2300 }
2301
2302 function getU(sampleA, sampleB) {
2303 return reduce(sampleA, function(total, xA) {
2304 return total + getScore(xA, sampleB);
2305 }, 0);
2306 }
2307
2308 function getZ(u) {
2309 return (u - ((size1 * size2) / 2)) / sqrt((size1 * size2 * (size1 + size2 + 1)) / 12);
2310 }
2311
2312 // exit early if comparing the same benchmark
2313 if (me == other) {
2314 return 0;
2315 }
2316 // reject the null hyphothesis the two samples come from the
2317 // same population (i.e. have the same median) if...
2318 if (size1 + size2 > 30) {
2319 // ...the z-stat is greater than 1.96 or less than -1.96
2320 // http://www.statisticslectures.com/topics/mannwhitneyu/
2321 zStat = getZ(u);
2322 return abs(zStat) > 1.96 ? (zStat > 0 ? -1 : 1) : 0;
2323 }
2324 // ...the U value is less than or equal the critical U value
2325 // http://www.geoib.com/mann-whitney-u-test.html
2326 critical = maxSize < 5 || minSize < 3 ? 0 : uTable[maxSize][minSize - 3];
2327 return u <= critical ? (u == u1 ? 1 : -1) : 0;
2328 }
2329
2330 /**
2331 * Reset properties and abort if running.
2332 *
2333 * @memberOf Benchmark
2334 * @returns {Object} The benchmark instance.
2335 */
2336 function reset() {
2337 var data,
2338 event,
2339 me = this,
2340 index = 0,
2341 changes = { 'length': 0 },
2342 queue = { 'length': 0 };
2343
2344 if (me.running && !calledBy.abort) {
2345 // no worries, `reset()` is called within `abort()`
2346 calledBy.reset = true;
2347 me.abort();
2348 delete calledBy.reset;
2349 }
2350 else {
2351 // a non-recursive solution to check if properties have changed
2352 // http://www.jslab.dk/articles/non.recursive.preorder.traversal.part4
2353 data = { 'destination': me, 'source': extend({}, me.constructor.prototype, me.options) };
2354 do {
2355 forOwn(data.source, function(value, key) {
2356 var changed,
2357 destination = data.destination,
2358 currValue = destination[key];
2359
2360 if (value && typeof value == 'object') {
2361 if (isClassOf(value, 'Array')) {
2362 // check if an array value has changed to a non-array value
2363 if (!isClassOf(currValue, 'Array')) {
2364 changed = currValue = [];
2365 }
2366 // or has changed its length
2367 if (currValue.length != value.length) {
2368 changed = currValue = currValue.slice(0, value.length);
2369 currValue.length = value.length;
2370 }
2371 }
2372 // check if an object has changed to a non-object value
2373 else if (!currValue || typeof currValue != 'object') {
2374 changed = currValue = {};
2375 }
2376 // register a changed object
2377 if (changed) {
2378 changes[changes.length++] = { 'destination': destination, 'key': key, 'value': currValue };
2379 }
2380 queue[queue.length++] = { 'destination': currValue, 'source': value };
2381 }
2382 // register a changed primitive
2383 else if (value !== currValue && !(value == null || isClassOf(value, 'Function'))) {
2384 changes[changes.length++] = { 'destination': destination, 'key': key, 'value': value };
2385 }
2386 });
2387 }
2388 while ((data = queue[index++]));
2389
2390 // if changed emit the `reset` event and if it isn't cancelled reset the benchmark
2391 if (changes.length && (me.emit(event = Event('reset')), !event.cancelled)) {
2392 forEach(changes, function(data) {
2393 data.destination[data.key] = data.value;
2394 });
2395 }
2396 }
2397 return me;
2398 }
2399
2400 /**
2401 * Displays relevant benchmark information when coerced to a string.
2402 *
2403 * @name toString
2404 * @memberOf Benchmark
2405 * @returns {String} A string representation of the benchmark instance.
2406 */
2407 function toStringBench() {
2408 var me = this,
2409 error = me.error,
2410 hz = me.hz,
2411 id = me.id,
2412 stats = me.stats,
2413 size = stats.sample.length,
2414 pm = support.java ? '+/-' : '\xb1',
2415 result = me.name || (isNaN(id) ? id : '<Test #' + id + '>');
2416
2417 if (error) {
2418 result += ': ' + join(error);
2419 } else {
2420 result += ' x ' + formatNumber(hz.toFixed(hz < 100 ? 2 : 0)) + ' ops/sec ' + pm +
2421 stats.rme.toFixed(2) + '% (' + size + ' run' + (size == 1 ? '' : 's') + ' sampled)';
2422 }
2423 return result;
2424 }
2425
2426 /*--------------------------------------------------------------------------*/
2427
2428 /**
2429 * Clocks the time taken to execute a test per cycle (secs).
2430 *
2431 * @private
2432 * @param {Object} bench The benchmark instance.
2433 * @returns {Number} The time taken.
2434 */
2435 function clock() {
2436 var applet,
2437 options = Benchmark.options,
2438 template = { 'begin': 's$=new n$', 'end': 'r$=(new n$-s$)/1e3', 'uid': uid },
2439 timers = [{ 'ns': timer.ns, 'res': max(0.0015, getRes('ms')), 'unit': 'ms' }];
2440
2441 // lazy define for hi-res timers
2442 clock = function(clone) {
2443 var deferred;
2444 if (clone instanceof Deferred) {
2445 deferred = clone;
2446 clone = deferred.benchmark;
2447 }
2448
2449 var bench = clone._original,
2450 fn = bench.fn,
2451 fnArg = deferred ? getFirstArgument(fn) || 'deferred' : '',
2452 stringable = isStringable(fn);
2453
2454 var source = {
2455 'setup': getSource(bench.setup, preprocess('m$.setup()')),
2456 'fn': getSource(fn, preprocess('m$.fn(' + fnArg + ')')),
2457 'fnArg': fnArg,
2458 'teardown': getSource(bench.teardown, preprocess('m$.teardown()'))
2459 };
2460
2461 var count = bench.count = clone.count,
2462 decompilable = support.decompilation || stringable,
2463 id = bench.id,
2464 isEmpty = !(source.fn || stringable),
2465 name = bench.name || (typeof id == 'number' ? '<Test #' + id + '>' : id),
2466 ns = timer.ns,
2467 result = 0;
2468
2469 // init `minTime` if needed
2470 clone.minTime = bench.minTime || (bench.minTime = bench.options.minTime = options.minTime);
2471
2472 // repair nanosecond timer
2473 // (some Chrome builds erase the `ns` variable after millions of executions)
2474 if (applet) {
2475 try {
2476 ns.nanoTime();
2477 } catch(e) {
2478 // use non-element to avoid issues with libs that augment them
2479 ns = timer.ns = new applet.Packages.nano;
2480 }
2481 }
2482
2483 // Compile in setup/teardown functions and the test loop.
2484 // Create a new compiled test, instead of using the cached `bench.compiled`,
2485 // to avoid potential engine optimizations enabled over the life of the test.
2486 var compiled = bench.compiled = createFunction(preprocess('t$'), interpolate(
2487 preprocess(deferred
2488 ? 'var d$=this,#{fnArg}=d$,m$=d$.benchmark._original,f$=m$.fn,su$=m$.setup,td$=m$.teardown;' +
2489 // when `deferred.cycles` is `0` then...
2490 'if(!d$.cycles){' +
2491 // set `deferred.fn`
2492 'd$.fn=function(){var #{fnArg}=d$;if(typeof f$=="function"){try{#{fn}\n}catch(e$){f$(d$)}}else{#{fn}\n}};' +
2493 // set `deferred.teardown`
2494 'd$.teardown=function(){d$.cycles=0;if(typeof td$=="function"){try{#{teardown}\n}catch(e$){td$()}}else{#{teardown}\n}};' +
2495 // execute the benchmark's `setup`
2496 'if(typeof su$=="function"){try{#{setup}\n}catch(e$){su$()}}else{#{setup}\n};' +
2497 // start timer
2498 't$.start(d$);' +
2499 // execute `deferred.fn` and return a dummy object
2500 '}d$.fn();return{}'
2501
2502 : 'var r$,s$,m$=this,f$=m$.fn,i$=m$.count,n$=t$.ns;#{setup}\n#{begin};' +
2503 'while(i$--){#{fn}\n}#{end};#{teardown}\nreturn{elapsed:r$,uid:"#{uid}"}'),
2504 source
2505 ));
2506
2507 try {
2508 if (isEmpty) {
2509 // Firefox may remove dead code from Function#toString results
2510 // http://bugzil.la/536085
2511 throw new Error('The test "' + name + '" is empty. This may be the result of dead code removal.');
2512 }
2513 else if (!deferred) {
2514 // pretest to determine if compiled code is exits early, usually by a
2515 // rogue `return` statement, by checking for a return object with the uid
2516 bench.count = 1;
2517 compiled = (compiled.call(bench, timer) || {}).uid == uid && compiled;
2518 bench.count = count;
2519 }
2520 } catch(e) {
2521 compiled = null;
2522 clone.error = e || new Error(String(e));
2523 bench.count = count;
2524 }
2525 // fallback when a test exits early or errors during pretest
2526 if (decompilable && !compiled && !deferred && !isEmpty) {
2527 compiled = createFunction(preprocess('t$'), interpolate(
2528 preprocess(
2529 (clone.error && !stringable
2530 ? 'var r$,s$,m$=this,f$=m$.fn,i$=m$.count'
2531 : 'function f$(){#{fn}\n}var r$,s$,m$=this,i$=m$.count'
2532 ) +
2533 ',n$=t$.ns;#{setup}\n#{begin};m$.f$=f$;while(i$--){m$.f$()}#{end};' +
2534 'delete m$.f$;#{teardown}\nreturn{elapsed:r$}'
2535 ),
2536 source
2537 ));
2538
2539 try {
2540 // pretest one more time to check for errors
2541 bench.count = 1;
2542 compiled.call(bench, timer);
2543 bench.compiled = compiled;
2544 bench.count = count;
2545 delete clone.error;
2546 }
2547 catch(e) {
2548 bench.count = count;
2549 if (clone.error) {
2550 compiled = null;
2551 } else {
2552 bench.compiled = compiled;
2553 clone.error = e || new Error(String(e));
2554 }
2555 }
2556 }
2557 // assign `compiled` to `clone` before calling in case a deferred benchmark
2558 // immediately calls `deferred.resolve()`
2559 clone.compiled = compiled;
2560 // if no errors run the full test loop
2561 if (!clone.error) {
2562 result = compiled.call(deferred || bench, timer).elapsed;
2563 }
2564 return result;
2565 };
2566
2567 /*------------------------------------------------------------------------*/
2568
2569 /**
2570 * Gets the current timer's minimum resolution (secs).
2571 */
2572 function getRes(unit) {
2573 var measured,
2574 begin,
2575 count = 30,
2576 divisor = 1e3,
2577 ns = timer.ns,
2578 sample = [];
2579
2580 // get average smallest measurable time
2581 while (count--) {
2582 if (unit == 'us') {
2583 divisor = 1e6;
2584 if (ns.stop) {
2585 ns.start();
2586 while (!(measured = ns.microseconds())) { }
2587 } else if (ns[perfName]) {
2588 divisor = 1e3;
2589 measured = Function('n', 'var r,s=n.' + perfName + '();while(!(r=n.' + perfName + '()-s)){};return r')(ns);
2590 } else {
2591 begin = ns();
2592 while (!(measured = ns() - begin)) { }
2593 }
2594 }
2595 else if (unit == 'ns') {
2596 divisor = 1e9;
2597 if (ns.nanoTime) {
2598 begin = ns.nanoTime();
2599 while (!(measured = ns.nanoTime() - begin)) { }
2600 } else {
2601 begin = (begin = ns())[0] + (begin[1] / divisor);
2602 while (!(measured = ((measured = ns())[0] + (measured[1] / divisor)) - begin)) { }
2603 divisor = 1;
2604 }
2605 }
2606 else {
2607 begin = new ns;
2608 while (!(measured = new ns - begin)) { }
2609 }
2610 // check for broken timers (nanoTime may have issues)
2611 // http://alivebutsleepy.srnet.cz/unreliable-system-nanotime/
2612 if (measured > 0) {
2613 sample.push(measured);
2614 } else {
2615 sample.push(Infinity);
2616 break;
2617 }
2618 }
2619 // convert to seconds
2620 return getMean(sample) / divisor;
2621 }
2622
2623 /**
2624 * Replaces all occurrences of `$` with a unique number and
2625 * template tokens with content.
2626 */
2627 function preprocess(code) {
2628 return interpolate(code, template).replace(/\$/g, /\d+/.exec(uid));
2629 }
2630
2631 /*------------------------------------------------------------------------*/
2632
2633 // detect nanosecond support from a Java applet
2634 each(doc && doc.applets || [], function(element) {
2635 return !(timer.ns = applet = 'nanoTime' in element && element);
2636 });
2637
2638 // check type in case Safari returns an object instead of a number
2639 try {
2640 if (typeof timer.ns.nanoTime() == 'number') {
2641 timers.push({ 'ns': timer.ns, 'res': getRes('ns'), 'unit': 'ns' });
2642 }
2643 } catch(e) { }
2644
2645 // detect Chrome's microsecond timer:
2646 // enable benchmarking via the --enable-benchmarking command
2647 // line switch in at least Chrome 7 to use chrome.Interval
2648 try {
2649 if ((timer.ns = new (window.chrome || window.chromium).Interval)) {
2650 timers.push({ 'ns': timer.ns, 'res': getRes('us'), 'unit': 'us' });
2651 }
2652 } catch(e) { }
2653
2654 // detect `performance.now` microsecond resolution timer
2655 if ((timer.ns = perfName && perfObject)) {
2656 timers.push({ 'ns': timer.ns, 'res': getRes('us'), 'unit': 'us' });
2657 }
2658
2659 // detect Node's nanosecond resolution timer available in Node >= 0.8
2660 if (processObject && typeof (timer.ns = processObject.hrtime) == 'function') {
2661 timers.push({ 'ns': timer.ns, 'res': getRes('ns'), 'unit': 'ns' });
2662 }
2663
2664 // detect Wade Simmons' Node microtime module
2665 if (microtimeObject && typeof (timer.ns = microtimeObject.now) == 'function') {
2666 timers.push({ 'ns': timer.ns, 'res': getRes('us'), 'unit': 'us' });
2667 }
2668
2669 // pick timer with highest resolution
2670 timer = reduce(timers, function(timer, other) {
2671 return other.res < timer.res ? other : timer;
2672 });
2673
2674 // remove unused applet
2675 if (timer.unit != 'ns' && applet) {
2676 applet = destroyElement(applet);
2677 }
2678 // error if there are no working timers
2679 if (timer.res == Infinity) {
2680 throw new Error('Benchmark.js was unable to find a working timer.');
2681 }
2682 // use API of chosen timer
2683 if (timer.unit == 'ns') {
2684 if (timer.ns.nanoTime) {
2685 extend(template, {
2686 'begin': 's$=n$.nanoTime()',
2687 'end': 'r$=(n$.nanoTime()-s$)/1e9'
2688 });
2689 } else {
2690 extend(template, {
2691 'begin': 's$=n$()',
2692 'end': 'r$=n$(s$);r$=r$[0]+(r$[1]/1e9)'
2693 });
2694 }
2695 }
2696 else if (timer.unit == 'us') {
2697 if (timer.ns.stop) {
2698 extend(template, {
2699 'begin': 's$=n$.start()',
2700 'end': 'r$=n$.microseconds()/1e6'
2701 });
2702 } else if (perfName) {
2703 extend(template, {
2704 'begin': 's$=n$.' + perfName + '()',
2705 'end': 'r$=(n$.' + perfName + '()-s$)/1e3'
2706 });
2707 } else {
2708 extend(template, {
2709 'begin': 's$=n$()',
2710 'end': 'r$=(n$()-s$)/1e6'
2711 });
2712 }
2713 }
2714
2715 // define `timer` methods
2716 timer.start = createFunction(preprocess('o$'),
2717 preprocess('var n$=this.ns,#{begin};o$.elapsed=0;o$.timeStamp=s$'));
2718
2719 timer.stop = createFunction(preprocess('o$'),
2720 preprocess('var n$=this.ns,s$=o$.timeStamp,#{end};o$.elapsed=r$'));
2721
2722 // resolve time span required to achieve a percent uncertainty of at most 1%
2723 // http://spiff.rit.edu/classes/phys273/uncert/uncert.html
2724 options.minTime || (options.minTime = max(timer.res / 2 / 0.01, 0.05));
2725 return clock.apply(null, arguments);
2726 }
2727
2728 /*--------------------------------------------------------------------------*/
2729
2730 /**
2731 * Computes stats on benchmark results.
2732 *
2733 * @private
2734 * @param {Object} bench The benchmark instance.
2735 * @param {Object} options The options object.
2736 */
2737 function compute(bench, options) {
2738 options || (options = {});
2739
2740 var async = options.async,
2741 elapsed = 0,
2742 initCount = bench.initCount,
2743 minSamples = bench.minSamples,
2744 queue = [],
2745 sample = bench.stats.sample;
2746
2747 /**
2748 * Adds a clone to the queue.
2749 */
2750 function enqueue() {
2751 queue.push(bench.clone({
2752 '_original': bench,
2753 'events': {
2754 'abort': [update],
2755 'cycle': [update],
2756 'error': [update],
2757 'start': [update]
2758 }
2759 }));
2760 }
2761
2762 /**
2763 * Updates the clone/original benchmarks to keep their data in sync.
2764 */
2765 function update(event) {
2766 var clone = this,
2767 type = event.type;
2768
2769 if (bench.running) {
2770 if (type == 'start') {
2771 // Note: `clone.minTime` prop is inited in `clock()`
2772 clone.count = bench.initCount;
2773 }
2774 else {
2775 if (type == 'error') {
2776 bench.error = clone.error;
2777 }
2778 if (type == 'abort') {
2779 bench.abort();
2780 bench.emit('cycle');
2781 } else {
2782 event.currentTarget = event.target = bench;
2783 bench.emit(event);
2784 }
2785 }
2786 } else if (bench.aborted) {
2787 // clear abort listeners to avoid triggering bench's abort/cycle again
2788 clone.events.abort.length = 0;
2789 clone.abort();
2790 }
2791 }
2792
2793 /**
2794 * Determines if more clones should be queued or if cycling should stop.
2795 */
2796 function evaluate(event) {
2797 var critical,
2798 df,
2799 mean,
2800 moe,
2801 rme,
2802 sd,
2803 sem,
2804 variance,
2805 clone = event.target,
2806 done = bench.aborted,
2807 now = +new Date,
2808 size = sample.push(clone.times.period),
2809 maxedOut = size >= minSamples && (elapsed += now - clone.times.timeStamp) / 1e3 > bench.maxTime,
2810 times = bench.times,
2811 varOf = function(sum, x) { return sum + pow(x - mean, 2); };
2812
2813 // exit early for aborted or unclockable tests
2814 if (done || clone.hz == Infinity) {
2815 maxedOut = !(size = sample.length = queue.length = 0);
2816 }
2817
2818 if (!done) {
2819 // sample mean (estimate of the population mean)
2820 mean = getMean(sample);
2821 // sample variance (estimate of the population variance)
2822 variance = reduce(sample, varOf, 0) / (size - 1) || 0;
2823 // sample standard deviation (estimate of the population standard deviation)
2824 sd = sqrt(variance);
2825 // standard error of the mean (a.k.a. the standard deviation of the sampling distribution of the sample mean)
2826 sem = sd / sqrt(size);
2827 // degrees of freedom
2828 df = size - 1;
2829 // critical value
2830 critical = tTable[Math.round(df) || 1] || tTable.infinity;
2831 // margin of error
2832 moe = sem * critical;
2833 // relative margin of error
2834 rme = (moe / mean) * 100 || 0;
2835
2836 extend(bench.stats, {
2837 'deviation': sd,
2838 'mean': mean,
2839 'moe': moe,
2840 'rme': rme,
2841 'sem': sem,
2842 'variance': variance
2843 });
2844
2845 // Abort the cycle loop when the minimum sample size has been collected
2846 // and the elapsed time exceeds the maximum time allowed per benchmark.
2847 // We don't count cycle delays toward the max time because delays may be
2848 // increased by browsers that clamp timeouts for inactive tabs.
2849 // https://developer.mozilla.org/en/window.setTimeout#Inactive_tabs
2850 if (maxedOut) {
2851 // reset the `initCount` in case the benchmark is rerun
2852 bench.initCount = initCount;
2853 bench.running = false;
2854 done = true;
2855 times.elapsed = (now - times.timeStamp) / 1e3;
2856 }
2857 if (bench.hz != Infinity) {
2858 bench.hz = 1 / mean;
2859 times.cycle = mean * bench.count;
2860 times.period = mean;
2861 }
2862 }
2863 // if time permits, increase sample size to reduce the margin of error
2864 if (queue.length < 2 && !maxedOut) {
2865 enqueue();
2866 }
2867 // abort the invoke cycle when done
2868 event.aborted = done;
2869 }
2870
2871 // init queue and begin
2872 enqueue();
2873 invoke(queue, {
2874 'name': 'run',
2875 'args': { 'async': async },
2876 'queued': true,
2877 'onCycle': evaluate,
2878 'onComplete': function() { bench.emit('complete'); }
2879 });
2880 }
2881
2882 /*--------------------------------------------------------------------------*/
2883
2884 /**
2885 * Cycles a benchmark until a run `count` can be established.
2886 *
2887 * @private
2888 * @param {Object} clone The cloned benchmark instance.
2889 * @param {Object} options The options object.
2890 */
2891 function cycle(clone, options) {
2892 options || (options = {});
2893
2894 var deferred;
2895 if (clone instanceof Deferred) {
2896 deferred = clone;
2897 clone = clone.benchmark;
2898 }
2899
2900 var clocked,
2901 cycles,
2902 divisor,
2903 event,
2904 minTime,
2905 period,
2906 async = options.async,
2907 bench = clone._original,
2908 count = clone.count,
2909 times = clone.times;
2910
2911 // continue, if not aborted between cycles
2912 if (clone.running) {
2913 // `minTime` is set to `Benchmark.options.minTime` in `clock()`
2914 cycles = ++clone.cycles;
2915 clocked = deferred ? deferred.elapsed : clock(clone);
2916 minTime = clone.minTime;
2917
2918 if (cycles > bench.cycles) {
2919 bench.cycles = cycles;
2920 }
2921 if (clone.error) {
2922 event = Event('error');
2923 event.message = clone.error;
2924 clone.emit(event);
2925 if (!event.cancelled) {
2926 clone.abort();
2927 }
2928 }
2929 }
2930
2931 // continue, if not errored
2932 if (clone.running) {
2933 // time taken to complete last test cycle
2934 bench.times.cycle = times.cycle = clocked;
2935 // seconds per operation
2936 period = bench.times.period = times.period = clocked / count;
2937 // ops per second
2938 bench.hz = clone.hz = 1 / period;
2939 // avoid working our way up to this next time
2940 bench.initCount = clone.initCount = count;
2941 // do we need to do another cycle?
2942 clone.running = clocked < minTime;
2943
2944 if (clone.running) {
2945 // tests may clock at `0` when `initCount` is a small number,
2946 // to avoid that we set its count to something a bit higher
2947 if (!clocked && (divisor = divisors[clone.cycles]) != null) {
2948 count = floor(4e6 / divisor);
2949 }
2950 // calculate how many more iterations it will take to achive the `minTime`
2951 if (count <= clone.count) {
2952 count += Math.ceil((minTime - clocked) / period);
2953 }
2954 clone.running = count != Infinity;
2955 }
2956 }
2957 // should we exit early?
2958 event = Event('cycle');
2959 clone.emit(event);
2960 if (event.aborted) {
2961 clone.abort();
2962 }
2963 // figure out what to do next
2964 if (clone.running) {
2965 // start a new cycle
2966 clone.count = count;
2967 if (deferred) {
2968 clone.compiled.call(deferred, timer);
2969 } else if (async) {
2970 delay(clone, function() { cycle(clone, options); });
2971 } else {
2972 cycle(clone);
2973 }
2974 }
2975 else {
2976 // fix TraceMonkey bug associated with clock fallbacks
2977 // http://bugzil.la/509069
2978 if (support.browser) {
2979 runScript(uid + '=1;delete ' + uid);
2980 }
2981 // done
2982 clone.emit('complete');
2983 }
2984 }
2985
2986 /*--------------------------------------------------------------------------*/
2987
2988 /**
2989 * Runs the benchmark.
2990 *
2991 * @memberOf Benchmark
2992 * @param {Object} [options={}] Options object.
2993 * @returns {Object} The benchmark instance.
2994 * @example
2995 *
2996 * // basic usage
2997 * bench.run();
2998 *
2999 * // or with options
3000 * bench.run({ 'async': true });
3001 */
3002 function run(options) {
3003 var me = this,
3004 event = Event('start');
3005
3006 // set `running` to `false` so `reset()` won't call `abort()`
3007 me.running = false;
3008 me.reset();
3009 me.running = true;
3010
3011 me.count = me.initCount;
3012 me.times.timeStamp = +new Date;
3013 me.emit(event);
3014
3015 if (!event.cancelled) {
3016 options = { 'async': ((options = options && options.async) == null ? me.async : options) && support.timeout };
3017
3018 // for clones created within `compute()`
3019 if (me._original) {
3020 if (me.defer) {
3021 Deferred(me);
3022 } else {
3023 cycle(me, options);
3024 }
3025 }
3026 // for original benchmarks
3027 else {
3028 compute(me, options);
3029 }
3030 }
3031 return me;
3032 }
3033
3034 /*--------------------------------------------------------------------------*/
3035
3036 // Firefox 1 erroneously defines variable and argument names of functions on
3037 // the function itself as non-configurable properties with `undefined` values.
3038 // The bugginess continues as the `Benchmark` constructor has an argument
3039 // named `options` and Firefox 1 will not assign a value to `Benchmark.options`,
3040 // making it non-writable in the process, unless it is the first property
3041 // assigned by for-in loop of `extend()`.
3042 extend(Benchmark, {
3043
3044 /**
3045 * The default options copied by benchmark instances.
3046 *
3047 * @static
3048 * @memberOf Benchmark
3049 * @type Object
3050 */
3051 'options': {
3052
3053 /**
3054 * A flag to indicate that benchmark cycles will execute asynchronously
3055 * by default.
3056 *
3057 * @memberOf Benchmark.options
3058 * @type Boolean
3059 */
3060 'async': false,
3061
3062 /**
3063 * A flag to indicate that the benchmark clock is deferred.
3064 *
3065 * @memberOf Benchmark.options
3066 * @type Boolean
3067 */
3068 'defer': false,
3069
3070 /**
3071 * The delay between test cycles (secs).
3072 * @memberOf Benchmark.options
3073 * @type Number
3074 */
3075 'delay': 0.005,
3076
3077 /**
3078 * Displayed by Benchmark#toString when a `name` is not available
3079 * (auto-generated if absent).
3080 *
3081 * @memberOf Benchmark.options
3082 * @type String
3083 */
3084 'id': undefined,
3085
3086 /**
3087 * The default number of times to execute a test on a benchmark's first cycle.
3088 *
3089 * @memberOf Benchmark.options
3090 * @type Number
3091 */
3092 'initCount': 1,
3093
3094 /**
3095 * The maximum time a benchmark is allowed to run before finishing (secs).
3096 * Note: Cycle delays aren't counted toward the maximum time.
3097 *
3098 * @memberOf Benchmark.options
3099 * @type Number
3100 */
3101 'maxTime': 5,
3102
3103 /**
3104 * The minimum sample size required to perform statistical analysis.
3105 *
3106 * @memberOf Benchmark.options
3107 * @type Number
3108 */
3109 'minSamples': 5,
3110
3111 /**
3112 * The time needed to reduce the percent uncertainty of measurement to 1% (secs).
3113 *
3114 * @memberOf Benchmark.options
3115 * @type Number
3116 */
3117 'minTime': 0,
3118
3119 /**
3120 * The name of the benchmark.
3121 *
3122 * @memberOf Benchmark.options
3123 * @type String
3124 */
3125 'name': undefined,
3126
3127 /**
3128 * An event listener called when the benchmark is aborted.
3129 *
3130 * @memberOf Benchmark.options
3131 * @type Function
3132 */
3133 'onAbort': undefined,
3134
3135 /**
3136 * An event listener called when the benchmark completes running.
3137 *
3138 * @memberOf Benchmark.options
3139 * @type Function
3140 */
3141 'onComplete': undefined,
3142
3143 /**
3144 * An event listener called after each run cycle.
3145 *
3146 * @memberOf Benchmark.options
3147 * @type Function
3148 */
3149 'onCycle': undefined,
3150
3151 /**
3152 * An event listener called when a test errors.
3153 *
3154 * @memberOf Benchmark.options
3155 * @type Function
3156 */
3157 'onError': undefined,
3158
3159 /**
3160 * An event listener called when the benchmark is reset.
3161 *
3162 * @memberOf Benchmark.options
3163 * @type Function
3164 */
3165 'onReset': undefined,
3166
3167 /**
3168 * An event listener called when the benchmark starts running.
3169 *
3170 * @memberOf Benchmark.options
3171 * @type Function
3172 */
3173 'onStart': undefined
3174 },
3175
3176 /**
3177 * Platform object with properties describing things like browser name,
3178 * version, and operating system.
3179 *
3180 * @static
3181 * @memberOf Benchmark
3182 * @type Object
3183 */
3184 'platform': req('platform') || window.platform || {
3185
3186 /**
3187 * The platform description.
3188 *
3189 * @memberOf Benchmark.platform
3190 * @type String
3191 */
3192 'description': window.navigator && navigator.userAgent || null,
3193
3194 /**
3195 * The name of the browser layout engine.
3196 *
3197 * @memberOf Benchmark.platform
3198 * @type String|Null
3199 */
3200 'layout': null,
3201
3202 /**
3203 * The name of the product hosting the browser.
3204 *
3205 * @memberOf Benchmark.platform
3206 * @type String|Null
3207 */
3208 'product': null,
3209
3210 /**
3211 * The name of the browser/environment.
3212 *
3213 * @memberOf Benchmark.platform
3214 * @type String|Null
3215 */
3216 'name': null,
3217
3218 /**
3219 * The name of the product's manufacturer.
3220 *
3221 * @memberOf Benchmark.platform
3222 * @type String|Null
3223 */
3224 'manufacturer': null,
3225
3226 /**
3227 * The name of the operating system.
3228 *
3229 * @memberOf Benchmark.platform
3230 * @type String|Null
3231 */
3232 'os': null,
3233
3234 /**
3235 * The alpha/beta release indicator.
3236 *
3237 * @memberOf Benchmark.platform
3238 * @type String|Null
3239 */
3240 'prerelease': null,
3241
3242 /**
3243 * The browser/environment version.
3244 *
3245 * @memberOf Benchmark.platform
3246 * @type String|Null
3247 */
3248 'version': null,
3249
3250 /**
3251 * Return platform description when the platform object is coerced to a string.
3252 *
3253 * @memberOf Benchmark.platform
3254 * @type Function
3255 * @returns {String} The platform description.
3256 */
3257 'toString': function() {
3258 return this.description || '';
3259 }
3260 },
3261
3262 /**
3263 * The semantic version number.
3264 *
3265 * @static
3266 * @memberOf Benchmark
3267 * @type String
3268 */
3269 'version': '1.0.0',
3270
3271 // an object of environment/feature detection flags
3272 'support': support,
3273
3274 // clone objects
3275 'deepClone': deepClone,
3276
3277 // iteration utility
3278 'each': each,
3279
3280 // augment objects
3281 'extend': extend,
3282
3283 // generic Array#filter
3284 'filter': filter,
3285
3286 // generic Array#forEach
3287 'forEach': forEach,
3288
3289 // generic own property iteration utility
3290 'forOwn': forOwn,
3291
3292 // converts a number to a comma-separated string
3293 'formatNumber': formatNumber,
3294
3295 // generic Object#hasOwnProperty
3296 // (trigger hasKey's lazy define before assigning it to Benchmark)
3297 'hasKey': (hasKey(Benchmark, ''), hasKey),
3298
3299 // generic Array#indexOf
3300 'indexOf': indexOf,
3301
3302 // template utility
3303 'interpolate': interpolate,
3304
3305 // invokes a method on each item in an array
3306 'invoke': invoke,
3307
3308 // generic Array#join for arrays and objects
3309 'join': join,
3310
3311 // generic Array#map
3312 'map': map,
3313
3314 // retrieves a property value from each item in an array
3315 'pluck': pluck,
3316
3317 // generic Array#reduce
3318 'reduce': reduce
3319 });
3320
3321 /*--------------------------------------------------------------------------*/
3322
3323 extend(Benchmark.prototype, {
3324
3325 /**
3326 * The number of times a test was executed.
3327 *
3328 * @memberOf Benchmark
3329 * @type Number
3330 */
3331 'count': 0,
3332
3333 /**
3334 * The number of cycles performed while benchmarking.
3335 *
3336 * @memberOf Benchmark
3337 * @type Number
3338 */
3339 'cycles': 0,
3340
3341 /**
3342 * The number of executions per second.
3343 *
3344 * @memberOf Benchmark
3345 * @type Number
3346 */
3347 'hz': 0,
3348
3349 /**
3350 * The compiled test function.
3351 *
3352 * @memberOf Benchmark
3353 * @type Function|String
3354 */
3355 'compiled': undefined,
3356
3357 /**
3358 * The error object if the test failed.
3359 *
3360 * @memberOf Benchmark
3361 * @type Object
3362 */
3363 'error': undefined,
3364
3365 /**
3366 * The test to benchmark.
3367 *
3368 * @memberOf Benchmark
3369 * @type Function|String
3370 */
3371 'fn': undefined,
3372
3373 /**
3374 * A flag to indicate if the benchmark is aborted.
3375 *
3376 * @memberOf Benchmark
3377 * @type Boolean
3378 */
3379 'aborted': false,
3380
3381 /**
3382 * A flag to indicate if the benchmark is running.
3383 *
3384 * @memberOf Benchmark
3385 * @type Boolean
3386 */
3387 'running': false,
3388
3389 /**
3390 * Compiled into the test and executed immediately **before** the test loop.
3391 *
3392 * @memberOf Benchmark
3393 * @type Function|String
3394 * @example
3395 *
3396 * // basic usage
3397 * var bench = Benchmark({
3398 * 'setup': function() {
3399 * var c = this.count,
3400 * element = document.getElementById('container');
3401 * while (c--) {
3402 * element.appendChild(document.createElement('div'));
3403 * }
3404 * },
3405 * 'fn': function() {
3406 * element.removeChild(element.lastChild);
3407 * }
3408 * });
3409 *
3410 * // compiles to something like:
3411 * var c = this.count,
3412 * element = document.getElementById('container');
3413 * while (c--) {
3414 * element.appendChild(document.createElement('div'));
3415 * }
3416 * var start = new Date;
3417 * while (count--) {
3418 * element.removeChild(element.lastChild);
3419 * }
3420 * var end = new Date - start;
3421 *
3422 * // or using strings
3423 * var bench = Benchmark({
3424 * 'setup': '\
3425 * var a = 0;\n\
3426 * (function() {\n\
3427 * (function() {\n\
3428 * (function() {',
3429 * 'fn': 'a += 1;',
3430 * 'teardown': '\
3431 * }())\n\
3432 * }())\n\
3433 * }())'
3434 * });
3435 *
3436 * // compiles to something like:
3437 * var a = 0;
3438 * (function() {
3439 * (function() {
3440 * (function() {
3441 * var start = new Date;
3442 * while (count--) {
3443 * a += 1;
3444 * }
3445 * var end = new Date - start;
3446 * }())
3447 * }())
3448 * }())
3449 */
3450 'setup': noop,
3451
3452 /**
3453 * Compiled into the test and executed immediately **after** the test loop.
3454 *
3455 * @memberOf Benchmark
3456 * @type Function|String
3457 */
3458 'teardown': noop,
3459
3460 /**
3461 * An object of stats including mean, margin or error, and standard deviation.
3462 *
3463 * @memberOf Benchmark
3464 * @type Object
3465 */
3466 'stats': {
3467
3468 /**
3469 * The margin of error.
3470 *
3471 * @memberOf Benchmark#stats
3472 * @type Number
3473 */
3474 'moe': 0,
3475
3476 /**
3477 * The relative margin of error (expressed as a percentage of the mean).
3478 *
3479 * @memberOf Benchmark#stats
3480 * @type Number
3481 */
3482 'rme': 0,
3483
3484 /**
3485 * The standard error of the mean.
3486 *
3487 * @memberOf Benchmark#stats
3488 * @type Number
3489 */
3490 'sem': 0,
3491
3492 /**
3493 * The sample standard deviation.
3494 *
3495 * @memberOf Benchmark#stats
3496 * @type Number
3497 */
3498 'deviation': 0,
3499
3500 /**
3501 * The sample arithmetic mean.
3502 *
3503 * @memberOf Benchmark#stats
3504 * @type Number
3505 */
3506 'mean': 0,
3507
3508 /**
3509 * The array of sampled periods.
3510 *
3511 * @memberOf Benchmark#stats
3512 * @type Array
3513 */
3514 'sample': [],
3515
3516 /**
3517 * The sample variance.
3518 *
3519 * @memberOf Benchmark#stats
3520 * @type Number
3521 */
3522 'variance': 0
3523 },
3524
3525 /**
3526 * An object of timing data including cycle, elapsed, period, start, and stop.
3527 *
3528 * @memberOf Benchmark
3529 * @type Object
3530 */
3531 'times': {
3532
3533 /**
3534 * The time taken to complete the last cycle (secs).
3535 *
3536 * @memberOf Benchmark#times
3537 * @type Number
3538 */
3539 'cycle': 0,
3540
3541 /**
3542 * The time taken to complete the benchmark (secs).
3543 *
3544 * @memberOf Benchmark#times
3545 * @type Number
3546 */
3547 'elapsed': 0,
3548
3549 /**
3550 * The time taken to execute the test once (secs).
3551 *
3552 * @memberOf Benchmark#times
3553 * @type Number
3554 */
3555 'period': 0,
3556
3557 /**
3558 * A timestamp of when the benchmark started (ms).
3559 *
3560 * @memberOf Benchmark#times
3561 * @type Number
3562 */
3563 'timeStamp': 0
3564 },
3565
3566 // aborts benchmark (does not record times)
3567 'abort': abort,
3568
3569 // creates a new benchmark using the same test and options
3570 'clone': clone,
3571
3572 // compares benchmark's hertz with another
3573 'compare': compare,
3574
3575 // executes listeners
3576 'emit': emit,
3577
3578 // get listeners
3579 'listeners': listeners,
3580
3581 // unregister listeners
3582 'off': off,
3583
3584 // register listeners
3585 'on': on,
3586
3587 // reset benchmark properties
3588 'reset': reset,
3589
3590 // runs the benchmark
3591 'run': run,
3592
3593 // pretty print benchmark info
3594 'toString': toStringBench
3595 });
3596
3597 /*--------------------------------------------------------------------------*/
3598
3599 extend(Deferred.prototype, {
3600
3601 /**
3602 * The deferred benchmark instance.
3603 *
3604 * @memberOf Benchmark.Deferred
3605 * @type Object
3606 */
3607 'benchmark': null,
3608
3609 /**
3610 * The number of deferred cycles performed while benchmarking.
3611 *
3612 * @memberOf Benchmark.Deferred
3613 * @type Number
3614 */
3615 'cycles': 0,
3616
3617 /**
3618 * The time taken to complete the deferred benchmark (secs).
3619 *
3620 * @memberOf Benchmark.Deferred
3621 * @type Number
3622 */
3623 'elapsed': 0,
3624
3625 /**
3626 * A timestamp of when the deferred benchmark started (ms).
3627 *
3628 * @memberOf Benchmark.Deferred
3629 * @type Number
3630 */
3631 'timeStamp': 0,
3632
3633 // cycles/completes the deferred benchmark
3634 'resolve': resolve
3635 });
3636
3637 /*--------------------------------------------------------------------------*/
3638
3639 extend(Event.prototype, {
3640
3641 /**
3642 * A flag to indicate if the emitters listener iteration is aborted.
3643 *
3644 * @memberOf Benchmark.Event
3645 * @type Boolean
3646 */
3647 'aborted': false,
3648
3649 /**
3650 * A flag to indicate if the default action is cancelled.
3651 *
3652 * @memberOf Benchmark.Event
3653 * @type Boolean
3654 */
3655 'cancelled': false,
3656
3657 /**
3658 * The object whose listeners are currently being processed.
3659 *
3660 * @memberOf Benchmark.Event
3661 * @type Object
3662 */
3663 'currentTarget': undefined,
3664
3665 /**
3666 * The return value of the last executed listener.
3667 *
3668 * @memberOf Benchmark.Event
3669 * @type Mixed
3670 */
3671 'result': undefined,
3672
3673 /**
3674 * The object to which the event was originally emitted.
3675 *
3676 * @memberOf Benchmark.Event
3677 * @type Object
3678 */
3679 'target': undefined,
3680
3681 /**
3682 * A timestamp of when the event was created (ms).
3683 *
3684 * @memberOf Benchmark.Event
3685 * @type Number
3686 */
3687 'timeStamp': 0,
3688
3689 /**
3690 * The event type.
3691 *
3692 * @memberOf Benchmark.Event
3693 * @type String
3694 */
3695 'type': ''
3696 });
3697
3698 /*--------------------------------------------------------------------------*/
3699
3700 /**
3701 * The default options copied by suite instances.
3702 *
3703 * @static
3704 * @memberOf Benchmark.Suite
3705 * @type Object
3706 */
3707 Suite.options = {
3708
3709 /**
3710 * The name of the suite.
3711 *
3712 * @memberOf Benchmark.Suite.options
3713 * @type String
3714 */
3715 'name': undefined
3716 };
3717
3718 /*--------------------------------------------------------------------------*/
3719
3720 extend(Suite.prototype, {
3721
3722 /**
3723 * The number of benchmarks in the suite.
3724 *
3725 * @memberOf Benchmark.Suite
3726 * @type Number
3727 */
3728 'length': 0,
3729
3730 /**
3731 * A flag to indicate if the suite is aborted.
3732 *
3733 * @memberOf Benchmark.Suite
3734 * @type Boolean
3735 */
3736 'aborted': false,
3737
3738 /**
3739 * A flag to indicate if the suite is running.
3740 *
3741 * @memberOf Benchmark.Suite
3742 * @type Boolean
3743 */
3744 'running': false,
3745
3746 /**
3747 * An `Array#forEach` like method.
3748 * Callbacks may terminate the loop by explicitly returning `false`.
3749 *
3750 * @memberOf Benchmark.Suite
3751 * @param {Function} callback The function called per iteration.
3752 * @returns {Object} The suite iterated over.
3753 */
3754 'forEach': methodize(forEach),
3755
3756 /**
3757 * An `Array#indexOf` like method.
3758 *
3759 * @memberOf Benchmark.Suite
3760 * @param {Mixed} value The value to search for.
3761 * @returns {Number} The index of the matched value or `-1`.
3762 */
3763 'indexOf': methodize(indexOf),
3764
3765 /**
3766 * Invokes a method on all benchmarks in the suite.
3767 *
3768 * @memberOf Benchmark.Suite
3769 * @param {String|Object} name The name of the method to invoke OR options object.
3770 * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the method with.
3771 * @returns {Array} A new array of values returned from each method invoked.
3772 */
3773 'invoke': methodize(invoke),
3774
3775 /**
3776 * Converts the suite of benchmarks to a string.
3777 *
3778 * @memberOf Benchmark.Suite
3779 * @param {String} [separator=','] A string to separate each element of the array.
3780 * @returns {String} The string.
3781 */
3782 'join': [].join,
3783
3784 /**
3785 * An `Array#map` like method.
3786 *
3787 * @memberOf Benchmark.Suite
3788 * @param {Function} callback The function called per iteration.
3789 * @returns {Array} A new array of values returned by the callback.
3790 */
3791 'map': methodize(map),
3792
3793 /**
3794 * Retrieves the value of a specified property from all benchmarks in the suite.
3795 *
3796 * @memberOf Benchmark.Suite
3797 * @param {String} property The property to pluck.
3798 * @returns {Array} A new array of property values.
3799 */
3800 'pluck': methodize(pluck),
3801
3802 /**
3803 * Removes the last benchmark from the suite and returns it.
3804 *
3805 * @memberOf Benchmark.Suite
3806 * @returns {Mixed} The removed benchmark.
3807 */
3808 'pop': [].pop,
3809
3810 /**
3811 * Appends benchmarks to the suite.
3812 *
3813 * @memberOf Benchmark.Suite
3814 * @returns {Number} The suite's new length.
3815 */
3816 'push': [].push,
3817
3818 /**
3819 * Sorts the benchmarks of the suite.
3820 *
3821 * @memberOf Benchmark.Suite
3822 * @param {Function} [compareFn=null] A function that defines the sort order.
3823 * @returns {Object} The sorted suite.
3824 */
3825 'sort': [].sort,
3826
3827 /**
3828 * An `Array#reduce` like method.
3829 *
3830 * @memberOf Benchmark.Suite
3831 * @param {Function} callback The function called per iteration.
3832 * @param {Mixed} accumulator Initial value of the accumulator.
3833 * @returns {Mixed} The accumulator.
3834 */
3835 'reduce': methodize(reduce),
3836
3837 // aborts all benchmarks in the suite
3838 'abort': abortSuite,
3839
3840 // adds a benchmark to the suite
3841 'add': add,
3842
3843 // creates a new suite with cloned benchmarks
3844 'clone': cloneSuite,
3845
3846 // executes listeners of a specified type
3847 'emit': emit,
3848
3849 // creates a new suite of filtered benchmarks
3850 'filter': filterSuite,
3851
3852 // get listeners
3853 'listeners': listeners,
3854
3855 // unregister listeners
3856 'off': off,
3857
3858 // register listeners
3859 'on': on,
3860
3861 // resets all benchmarks in the suite
3862 'reset': resetSuite,
3863
3864 // runs all benchmarks in the suite
3865 'run': runSuite,
3866
3867 // array methods
3868 'concat': concat,
3869
3870 'reverse': reverse,
3871
3872 'shift': shift,
3873
3874 'slice': slice,
3875
3876 'splice': splice,
3877
3878 'unshift': unshift
3879 });
3880
3881 /*--------------------------------------------------------------------------*/
3882
3883 // expose Deferred, Event and Suite
3884 extend(Benchmark, {
3885 'Deferred': Deferred,
3886 'Event': Event,
3887 'Suite': Suite
3888 });
3889
3890 // expose Benchmark
3891 // some AMD build optimizers, like r.js, check for specific condition patterns like the following:
3892 if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
3893 // define as an anonymous module so, through path mapping, it can be aliased
3894 define(function() {
3895 return Benchmark;
3896 });
3897 }
3898 // check for `exports` after `define` in case a build optimizer adds an `exports` object
3899 else if (freeExports) {
3900 // in Node.js or RingoJS v0.8.0+
3901 if (typeof module == 'object' && module && module.exports == freeExports) {
3902 (module.exports = Benchmark).Benchmark = Benchmark;
3903 }
3904 // in Narwhal or RingoJS v0.7.0-
3905 else {
3906 freeExports.Benchmark = Benchmark;
3907 }
3908 }
3909 // in a browser or Rhino
3910 else {
3911 // use square bracket notation so Closure Compiler won't munge `Benchmark`
3912 // http://code.google.com/closure/compiler/docs/api-tutorial3.html#export
3913 window['Benchmark'] = Benchmark;
3914 }
3915
3916 // trigger clock's lazy define early to avoid a security error
3917 if (support.air) {
3918 clock({ '_original': { 'fn': noop, 'count': 1, 'options': {} } });
3919 }
3920}(this));
3921
3922}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
3923},{"_process":5}],2:[function(require,module,exports){
3924// jshint newcap: false
3925(function (root, factory) {
3926 if (typeof define === 'function' && define.amd) {
3927 define([], factory); // AMD. Register as an anonymous module.
3928 } else if (typeof exports === 'object') {
3929 module.exports = factory(); // NodeJS
3930 } else { // Browser globals (root is window)
3931 root.snabbdom = factory();
3932 }
3933}(this, function () {
3934
3935'use strict';
3936
3937var isArr = Array.isArray;
3938
3939function isString(s) { return typeof s === 'string'; }
3940function isPrimitive(s) { return typeof s === 'string' || typeof s === 'number'; }
3941function isUndef(s) { return s === undefined; }
3942
3943function VNode(tag, props, children, text, elm) {
3944 return {tag: tag, props: props, children: children, text: text, elm: elm};
3945}
3946
3947function emptyNodeAt(elm) {
3948 return VNode(elm.tagName, {style: {}, class: {}}, [], undefined, elm);
3949}
3950var frag = document.createDocumentFragment();
3951var emptyNode = VNode(undefined, {style: {}, class: {}}, [], undefined);
3952
3953function h(selector, b, c) {
3954 var props = {}, children, tag, i;
3955 if (arguments.length === 3) {
3956 props = b; children = isPrimitive(c) ? [c] : c;
3957 } else if (arguments.length === 2) {
3958 if (isArr(b)) { children = b; }
3959 else if (isPrimitive(b)) { children = [b]; }
3960 else { props = b; }
3961 }
3962 // Parse selector
3963 var hashIdx = selector.indexOf('#');
3964 var dotIdx = selector.indexOf('.', hashIdx);
3965 var hash = hashIdx > 0 ? hashIdx : selector.length;
3966 var dot = dotIdx > 0 ? dotIdx : selector.length;
3967 tag = selector.slice(0, Math.min(hash, dot));
3968 if (hash < dot) props.id = selector.slice(hash + 1, dot);
3969 if (dotIdx > 0) props.className = selector.slice(dot+1).replace(/\./g, ' ');
3970
3971 if (isArr(children)) {
3972 for (i = 0; i < children.length; ++i) {
3973 if (isPrimitive(children[i])) children[i] = VNode(undefined, undefined, undefined, children[i]);
3974 }
3975 }
3976 return VNode(tag, props, children, undefined, undefined);
3977}
3978
3979function setStyles(elm, styles) {
3980 for (var key in styles) {
3981 elm.style[key] = styles[key];
3982 }
3983}
3984
3985function updateProps(elm, oldProps, props) {
3986 var key, val, name, on;
3987 for (key in props) {
3988 val = props[key];
3989 if (key === 'style') {
3990 for (name in val) {
3991 on = val[name];
3992 if (on !== oldProps.style[name]) {
3993 elm.style[name] = val[name];
3994 }
3995 }
3996 } else if (key === 'class') {
3997 for (name in val) {
3998 on = val[name];
3999 if (on !== oldProps.class[name]) {
4000 elm.classList[on ? 'add' : 'remove'](name);
4001 }
4002 }
4003 } else {
4004 elm[key] = val;
4005 }
4006 }
4007}
4008
4009function createElm(vnode) {
4010 var elm;
4011 if (isUndef(vnode.text)) {
4012 elm = document.createElement(vnode.tag);
4013 updateProps(elm, emptyNode.props, vnode.props);
4014 var children = vnode.children;
4015 if (isArr(children)) {
4016 for (var i = 0; i < vnode.children.length; ++i) {
4017 elm.appendChild(createElm(children[i]));
4018 }
4019 }
4020 } else {
4021 elm = document.createTextNode(vnode.text);
4022 }
4023 vnode.elm = elm;
4024 return elm;
4025}
4026
4027function getKey(vnode) {
4028 return isUndef(vnode.props) ? undefined : vnode.props.key;
4029}
4030
4031function sameVnode(vnode1, vnode2) {
4032 return isUndef(vnode1.key) ? vnode1.tag === vnode2.tag
4033 : getKey(vnode1) === getKey(vnode2);
4034}
4035
4036function createKeyToOldIdx(children, beginIdx, endIdx) {
4037 var i, map = {};
4038 for (i = beginIdx; i <= endIdx; ++i) {
4039 var ch = children[i];
4040 if (!isUndef(ch.props) && !isUndef(ch.props.key)) {
4041 map[ch.props.key] = i;
4042 }
4043 }
4044 return map;
4045}
4046
4047function updateChildren(parentElm, oldCh, newCh) {
4048 if (isUndef(oldCh) && isUndef(newCh)) {
4049 return; // Neither new nor old element has any children
4050 }
4051 var oldStartIdx = 0, oldEndIdx = oldCh.length - 1;
4052 var newStartIdx = 0, newEndIdx = newCh.length - 1;
4053 var oldStartVnode = oldCh[0], oldEndVnode = oldCh[oldEndIdx];
4054 var newStartVnode = newCh[0], newEndVnode = newCh[newEndIdx];
4055
4056 var oldKeyToIdx;
4057
4058 while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
4059 if (isUndef(oldStartVnode)) {
4060 oldStartVnode = oldCh[++oldStartIdx]; // Vnode has been moved left
4061 } else if (isUndef(oldEndVnode)) {
4062 oldEndVnode = oldCh[--oldEndIdx];
4063 } else if (sameVnode(oldStartVnode, newStartVnode)) {
4064 patchElm(oldStartVnode, newStartVnode);
4065 oldStartVnode = oldCh[++oldStartIdx];
4066 newStartVnode = newCh[++newStartIdx];
4067 } else if (sameVnode(oldEndVnode, newEndVnode)) {
4068 patchElm(oldEndVnode, newEndVnode);
4069 oldEndVnode = oldCh[--oldEndIdx];
4070 newEndVnode = newCh[--newEndIdx];
4071 } else if (!isUndef(oldStartVnode) && !isUndef(newEndVnode) &&
4072 sameVnode(oldStartVnode, newEndVnode)) { // Vnode moved right
4073 patchElm(oldStartVnode, newEndVnode);
4074 parentElm.insertBefore(oldStartVnode.elm, oldEndVnode.elm.nextSibling);
4075 oldStartVnode = oldCh[++oldStartIdx];
4076 newEndVnode = newCh[--newEndIdx];
4077 } else if (!isUndef(oldEndVnode) && !isUndef(newStartVnode) &&
4078 sameVnode(oldEndVnode, newStartVnode)) { // Vnode moved left
4079 patchElm(oldEndVnode, newStartVnode);
4080 parentElm.insertBefore(oldEndVnode.elm, oldStartVnode.elm);
4081 oldEndVnode = oldCh[--oldEndIdx];
4082 newStartVnode = newCh[++newStartIdx];
4083 } else {
4084 if (isUndef(oldKeyToIdx)) oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx);
4085 var idxInOld = oldKeyToIdx[getKey(newStartVnode)];
4086 if (isUndef(idxInOld)) { // New element
4087 createElm(newStartVnode);
4088 parentElm.insertBefore(newStartVnode.elm, oldStartVnode.elm);
4089 newStartVnode = newCh[++newStartIdx];
4090 } else {
4091 var elmToMove = oldCh[idxInOld];
4092 patchElm(elmToMove, newStartVnode);
4093 oldCh[idxInOld] = undefined;
4094 parentElm.insertBefore(elmToMove.elm, oldStartVnode.elm);
4095 newStartVnode = newCh[++newStartIdx];
4096 }
4097 }
4098 }
4099 if (oldStartIdx > oldEndIdx) { // Done with old elements
4100 for (; newStartIdx <= newEndIdx; ++newStartIdx) {
4101 frag.appendChild(createElm(newCh[newStartIdx]));
4102 }
4103 if (isUndef(oldStartVnode)) {
4104 parentElm.appendChild(frag);
4105 } else {
4106 parentElm.insertBefore(frag, oldStartVnode.elm);
4107 }
4108 } else if (newStartIdx > newEndIdx) { // Done with new elements
4109 for (; oldStartIdx <= oldEndIdx; ++oldStartIdx) {
4110 var ch = oldCh[oldStartIdx];
4111 if (!isUndef(ch)) {
4112 parentElm.removeChild(oldCh[oldStartIdx].elm);
4113 oldCh[oldStartIdx].elm = undefined;
4114 }
4115 }
4116 }
4117}
4118
4119function patchElm(oldVnode, newVnode) {
4120 var elm = newVnode.elm = oldVnode.elm;
4121 if (isUndef(newVnode.text)) {
4122 updateProps(elm, oldVnode.props, newVnode.props);
4123 updateChildren(elm, oldVnode.children, newVnode.children);
4124 } else {
4125 if (oldVnode.text !== newVnode.text) {
4126 elm.textContent = newVnode.text;
4127 }
4128 }
4129 return newVnode;
4130}
4131
4132return {h: h, createElm: createElm, patchElm: patchElm, patch: patchElm, emptyNodeAt: emptyNodeAt, emptyNode: emptyNode};
4133
4134}));
4135
4136},{}],3:[function(require,module,exports){
4137(function (global){
4138var Benchmark = require('benchmark');
4139var a = require('../snabbdom.js');
4140var b = require('../oldsnabbdom.js');
4141
4142global.a = a;
4143global.b = b;
4144
4145var suite = new Benchmark.Suite();
4146
4147a.spanNum = function spanNum(n) {
4148 return a.h('span', {key: n}, n.toString());
4149};
4150
4151b.spanNum = function spanNum(n) {
4152 return b.h('span', {key: n}, n.toString());
4153};
4154
4155var elms = global.elms = 10;
4156var arr = global.arr = [];
4157for (var n = 0; n < elms; ++n) { arr[n] = n; }
4158
4159document.addEventListener('DOMContentLoaded', function() {
4160 var elm = global.elm = document.getElementById('container');
4161 // add tests
4162 suite.add('a/ insert first', {
4163 setup: function() {
4164 var vnode1 = a.h('div', arr.map(a.spanNum));
4165 var vnode2 = a.h('div', ['new'].concat(arr).map(a.spanNum));
4166 },
4167 fn: function() {
4168 var emptyNode = a.emptyNodeAt(elm);
4169 a.patch(emptyNode, vnode1);
4170 a.patch(vnode1, vnode2);
4171 a.patch(vnode2, a.emptyNode);
4172 },
4173 })
4174 .add('b/ insert first', {
4175 setup: function() {
4176 var vnode1 = b.h('div', arr.map(b.spanNum));
4177 var vnode2 = b.h('div', ['new'].concat(arr).map(b.spanNum));
4178 },
4179 fn: function() {
4180 var emptyNode = b.emptyNodeAt(elm);
4181 b.patch(emptyNode, vnode1);
4182 b.patch(vnode1, vnode2);
4183 b.patch(vnode2, b.emptyNode);
4184 },
4185 })
4186 // add listeners
4187 .on('cycle', function(event) {
4188 console.log(String(event.target));
4189 })
4190 .on('complete', function() {
4191 console.log('Fastest is ' + this.filter('fastest').pluck('name'));
4192 })
4193 // run async
4194 .run({async: true});
4195});
4196
4197}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
4198},{"../oldsnabbdom.js":2,"../snabbdom.js":4,"benchmark":1}],4:[function(require,module,exports){
4199// jshint newcap: false
4200(function (root, factory) {
4201 if (typeof define === 'function' && define.amd) {
4202 define([], factory); // AMD. Register as an anonymous module.
4203 } else if (typeof exports === 'object') {
4204 module.exports = factory(); // NodeJS
4205 } else { // Browser globals (root is window)
4206 root.snabbdom = factory();
4207 }
4208}(this, function () {
4209
4210'use strict';
4211
4212var isArr = Array.isArray;
4213
4214function isString(s) { return typeof s === 'string'; }
4215function isPrimitive(s) { return typeof s === 'string' || typeof s === 'number'; }
4216function isUndef(s) { return s === undefined; }
4217
4218function VNode(tag, props, children, text, elm) {
4219 var key = !isUndef(props) ? props.key : undefined;
4220 return {tag: tag, props: props, children: children,
4221 text: text, elm: elm, key: key};
4222}
4223
4224function emptyNodeAt(elm) {
4225 return VNode(elm.tagName, {style: {}, class: {}}, [], undefined, elm);
4226}
4227var frag = document.createDocumentFragment();
4228var emptyNode = VNode(undefined, {style: {}, class: {}}, [], undefined);
4229
4230function h(selector, b, c) {
4231 var props = {}, children, tag, text, i;
4232 if (arguments.length === 3) {
4233 props = b;
4234 if (isArr(c)) { children = c; }
4235 else if (isPrimitive(c)) { text = c; }
4236 } else if (arguments.length === 2) {
4237 if (isArr(b)) { children = b; }
4238 else if (isPrimitive(b)) { text = b; }
4239 else { props = b; }
4240 }
4241 // Parse selector
4242 var hashIdx = selector.indexOf('#');
4243 var dotIdx = selector.indexOf('.', hashIdx);
4244 var hash = hashIdx > 0 ? hashIdx : selector.length;
4245 var dot = dotIdx > 0 ? dotIdx : selector.length;
4246 tag = selector.slice(0, Math.min(hash, dot));
4247 if (hash < dot) props.id = selector.slice(hash + 1, dot);
4248 if (dotIdx > 0) props.className = selector.slice(dot+1).replace(/\./g, ' ');
4249
4250 if (isArr(children)) {
4251 for (i = 0; i < children.length; ++i) {
4252 if (isPrimitive(children[i])) children[i] = VNode(undefined, undefined, undefined, children[i]);
4253 }
4254 }
4255 return VNode(tag, props, children, text, undefined);
4256}
4257
4258function updateProps(elm, oldProps, props) {
4259 var key, val, name, on;
4260 for (key in props) {
4261 val = props[key];
4262 if (key === 'style') {
4263 for (name in val) {
4264 on = val[name];
4265 if (on !== oldProps.style[name]) {
4266 elm.style[name] = val[name];
4267 }
4268 }
4269 } else if (key === 'class') {
4270 for (name in val) {
4271 on = val[name];
4272 if (on !== oldProps.class[name]) {
4273 elm.classList[on ? 'add' : 'remove'](name);
4274 }
4275 }
4276 } else if (key !== 'key') {
4277 elm[key] = val;
4278 }
4279 }
4280}
4281
4282function createElm(vnode) {
4283 var elm, children;
4284 if (!isUndef(vnode.tag)) {
4285 elm = document.createElement(vnode.tag);
4286 if (!isUndef(vnode.tag)) {
4287 updateProps(elm, emptyNode.props, vnode.props);
4288 }
4289 children = vnode.children;
4290 if (isArr(children)) {
4291 for (var i = 0; i < vnode.children.length; ++i) {
4292 elm.appendChild(createElm(children[i]));
4293 }
4294 } else if (isPrimitive(vnode.text)) {
4295 elm.textContent = vnode.text;
4296 }
4297 } else {
4298 elm = document.createTextNode(vnode.text);
4299 }
4300 vnode.elm = elm;
4301 return elm;
4302}
4303
4304function sameVnode(vnode1, vnode2) {
4305 return vnode1.key === vnode2.key && vnode1.tag === vnode2.tag;
4306}
4307
4308function createKeyToOldIdx(children, beginIdx, endIdx) {
4309 var i, map = {};
4310 for (i = beginIdx; i <= endIdx; ++i) {
4311 var ch = children[i];
4312 if (!isUndef(ch.props) && !isUndef(ch.props.key)) {
4313 map[ch.props.key] = i;
4314 }
4315 }
4316 return map;
4317}
4318
4319function updateChildren(parentElm, oldCh, newCh) {
4320 var oldStartIdx = 0, oldEndIdx, oldStartVnode, oldEndVnode;
4321 if (isUndef(oldCh)) {
4322 oldEndIdx = -1;
4323 } else {
4324 oldEndIdx = oldCh.length - 1;
4325 oldStartVnode = oldCh[0];
4326 oldEndVnode = oldCh[oldEndIdx];
4327 }
4328
4329 var newStartIdx = 0, newEndIdx, newStartVnode, newEndVnode;
4330 if (isUndef(newCh)) {
4331 newEndIdx = -1;
4332 } else {
4333 newEndIdx = newCh.length - 1;
4334 newStartVnode = newCh[0];
4335 newEndVnode = newCh[newEndIdx];
4336 }
4337
4338 var oldKeyToIdx;
4339
4340 while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
4341 if (isUndef(oldStartVnode)) {
4342 oldStartVnode = oldCh[++oldStartIdx]; // Vnode has been moved left
4343 } else if (isUndef(oldEndVnode)) {
4344 oldEndVnode = oldCh[--oldEndIdx];
4345 } else if (sameVnode(oldStartVnode, newStartVnode)) {
4346 while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx &&
4347 !isUndef(oldStartVnode) && sameVnode(oldStartVnode, newStartVnode)) {
4348 patchElm(oldStartVnode, newStartVnode);
4349 oldStartVnode = oldCh[++oldStartIdx];
4350 newStartVnode = newCh[++newStartIdx];
4351 }
4352 } else if (sameVnode(oldEndVnode, newEndVnode)) {
4353 while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx &&
4354 !isUndef(oldEndVnode) && sameVnode(oldEndVnode, newEndVnode)) {
4355 patchElm(oldEndVnode, newEndVnode);
4356 oldEndVnode = oldCh[--oldEndIdx];
4357 newEndVnode = newCh[--newEndIdx];
4358 }
4359 } else if (!isUndef(oldStartVnode) && !isUndef(newEndVnode) &&
4360 sameVnode(oldStartVnode, newEndVnode)) { // Vnode moved right
4361 patchElm(oldStartVnode, newEndVnode);
4362 parentElm.insertBefore(oldStartVnode.elm, oldEndVnode.elm.nextSibling);
4363 oldStartVnode = oldCh[++oldStartIdx];
4364 newEndVnode = newCh[--newEndIdx];
4365 } else if (!isUndef(oldEndVnode) && !isUndef(newStartVnode) &&
4366 sameVnode(oldEndVnode, newStartVnode)) { // Vnode moved left
4367 patchElm(oldEndVnode, newStartVnode);
4368 parentElm.insertBefore(oldEndVnode.elm, oldStartVnode.elm);
4369 oldEndVnode = oldCh[--oldEndIdx];
4370 newStartVnode = newCh[++newStartIdx];
4371 } else {
4372 if (isUndef(oldKeyToIdx)) oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx);
4373 var idxInOld = oldKeyToIdx[newStartVnode.key];
4374 if (isUndef(idxInOld)) { // New element
4375 createElm(newStartVnode);
4376 parentElm.insertBefore(newStartVnode.elm, oldStartVnode.elm);
4377 newStartVnode = newCh[++newStartIdx];
4378 } else {
4379 var elmToMove = oldCh[idxInOld];
4380 patchElm(elmToMove, newStartVnode);
4381 oldCh[idxInOld] = undefined;
4382 parentElm.insertBefore(elmToMove.elm, oldStartVnode.elm);
4383 newStartVnode = newCh[++newStartIdx];
4384 }
4385 }
4386 }
4387 if (oldStartIdx > oldEndIdx) { // Done with old elements
4388 for (; newStartIdx <= newEndIdx; ++newStartIdx) {
4389 frag.appendChild(createElm(newCh[newStartIdx]));
4390 }
4391 if (isUndef(oldStartVnode)) {
4392 parentElm.appendChild(frag);
4393 } else {
4394 parentElm.insertBefore(frag, oldStartVnode.elm);
4395 }
4396 } else if (newStartIdx > newEndIdx) { // Done with new elements
4397 for (; oldStartIdx <= oldEndIdx; ++oldStartIdx) {
4398 var ch = oldCh[oldStartIdx];
4399 if (!isUndef(ch)) {
4400 parentElm.removeChild(oldCh[oldStartIdx].elm);
4401 oldCh[oldStartIdx].elm = undefined;
4402 }
4403 }
4404 }
4405}
4406
4407function patchElm(oldVnode, newVnode) {
4408 var elm = newVnode.elm = oldVnode.elm;
4409 if (!isUndef(newVnode.props)) {
4410 updateProps(elm, oldVnode.props, newVnode.props);
4411 }
4412 if (isUndef(newVnode.text)) {
4413 updateChildren(elm, oldVnode.children, newVnode.children);
4414 } else {
4415 if (oldVnode.text !== newVnode.text) elm.textContent = newVnode.text;
4416 }
4417 return newVnode;
4418}
4419
4420return {h: h, createElm: createElm, patchElm: patchElm, patch: patchElm, emptyNodeAt: emptyNodeAt, emptyNode: emptyNode};
4421
4422}));
4423
4424},{}],5:[function(require,module,exports){
4425// shim for using process in browser
4426
4427var process = module.exports = {};
4428
4429process.nextTick = (function () {
4430 var canSetImmediate = typeof window !== 'undefined'
4431 && window.setImmediate;
4432 var canMutationObserver = typeof window !== 'undefined'
4433 && window.MutationObserver;
4434 var canPost = typeof window !== 'undefined'
4435 && window.postMessage && window.addEventListener
4436 ;
4437
4438 if (canSetImmediate) {
4439 return function (f) { return window.setImmediate(f) };
4440 }
4441
4442 var queue = [];
4443
4444 if (canMutationObserver) {
4445 var hiddenDiv = document.createElement("div");
4446 var observer = new MutationObserver(function () {
4447 var queueList = queue.slice();
4448 queue.length = 0;
4449 queueList.forEach(function (fn) {
4450 fn();
4451 });
4452 });
4453
4454 observer.observe(hiddenDiv, { attributes: true });
4455
4456 return function nextTick(fn) {
4457 if (!queue.length) {
4458 hiddenDiv.setAttribute('yes', 'no');
4459 }
4460 queue.push(fn);
4461 };
4462 }
4463
4464 if (canPost) {
4465 window.addEventListener('message', function (ev) {
4466 var source = ev.source;
4467 if ((source === window || source === null) && ev.data === 'process-tick') {
4468 ev.stopPropagation();
4469 if (queue.length > 0) {
4470 var fn = queue.shift();
4471 fn();
4472 }
4473 }
4474 }, true);
4475
4476 return function nextTick(fn) {
4477 queue.push(fn);
4478 window.postMessage('process-tick', '*');
4479 };
4480 }
4481
4482 return function nextTick(fn) {
4483 setTimeout(fn, 0);
4484 };
4485})();
4486
4487process.title = 'browser';
4488process.browser = true;
4489process.env = {};
4490process.argv = [];
4491
4492function noop() {}
4493
4494process.on = noop;
4495process.addListener = noop;
4496process.once = noop;
4497process.off = noop;
4498process.removeListener = noop;
4499process.removeAllListeners = noop;
4500process.emit = noop;
4501
4502process.binding = function (name) {
4503 throw new Error('process.binding is not supported');
4504};
4505
4506// TODO(shtylman)
4507process.cwd = function () { return '/' };
4508process.chdir = function (dir) {
4509 throw new Error('process.chdir is not supported');
4510};
4511
4512},{}]},{},[3]);