blob: 3ecce1d06bf4b65115cd0c31d4f08e4c14892126 [file] [log] [blame]
Akron7524be12016-06-01 17:31:33 +02001/*!
2 * Benchmark.js v2.0.0 <http://benchmarkjs.com/>
3 * Copyright 2010-2016 Mathias Bynens <http://mths.be/>
4 * Based on JSLitmus.js, copyright Robert Kieffer <http://broofa.com/>
5 * Modified by John-David Dalton <http://allyoucanleet.com/>
6 * Available under MIT license <http://mths.be/mit>
7 */
8;(function() {
9 'use strict';
10
11 /** Used as a safe reference for `undefined` in pre ES5 environments. */
12 var undefined;
13
14 /** Used to determine if values are of the language type Object. */
15 var objectTypes = {
16 'function': true,
17 'object': true
18 };
19
20 /** Used as a reference to the global object. */
21 var root = (objectTypes[typeof window] && window) || this;
22
23 /** Detect free variable `define`. */
24 var freeDefine = typeof define == 'function' && typeof define.amd == 'object' && define.amd && define;
25
26 /** Detect free variable `exports`. */
27 var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports;
28
29 /** Detect free variable `module`. */
30 var freeModule = objectTypes[typeof module] && module && !module.nodeType && module;
31
32 /** Detect free variable `global` from Node.js or Browserified code and use it as `root`. */
33 var freeGlobal = freeExports && freeModule && typeof global == 'object' && global;
34 if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal || freeGlobal.self === freeGlobal)) {
35 root = freeGlobal;
36 }
37
38 /** Detect free variable `require`. */
39 var freeRequire = typeof require == 'function' && require;
40
41 /** Used to assign each benchmark an incremented id. */
42 var counter = 0;
43
44 /** Detect the popular CommonJS extension `module.exports`. */
45 var moduleExports = freeModule && freeModule.exports === freeExports && freeExports;
46
47 /** Used to detect primitive types. */
48 var rePrimitive = /^(?:boolean|number|string|undefined)$/;
49
50 /** Used to make every compiled test unique. */
51 var uidCounter = 0;
52
53 /** Used to assign default `context` object properties. */
54 var contextProps = [
55 'Array', 'Date', 'Function', 'Math', 'Object', 'RegExp', 'String', '_',
56 'clearTimeout', 'chrome', 'chromium', 'document', 'navigator', 'phantom',
57 'platform', 'process', 'runtime', 'setTimeout'
58 ];
59
60 /** Used to avoid hz of Infinity. */
61 var divisors = {
62 '1': 4096,
63 '2': 512,
64 '3': 64,
65 '4': 8,
66 '5': 0
67 };
68
69 /**
70 * T-Distribution two-tailed critical values for 95% confidence.
71 * For more info see http://www.itl.nist.gov/div898/handbook/eda/section3/eda3672.htm.
72 */
73 var tTable = {
74 '1': 12.706, '2': 4.303, '3': 3.182, '4': 2.776, '5': 2.571, '6': 2.447,
75 '7': 2.365, '8': 2.306, '9': 2.262, '10': 2.228, '11': 2.201, '12': 2.179,
76 '13': 2.16, '14': 2.145, '15': 2.131, '16': 2.12, '17': 2.11, '18': 2.101,
77 '19': 2.093, '20': 2.086, '21': 2.08, '22': 2.074, '23': 2.069, '24': 2.064,
78 '25': 2.06, '26': 2.056, '27': 2.052, '28': 2.048, '29': 2.045, '30': 2.042,
79 'infinity': 1.96
80 };
81
82 /**
83 * Critical Mann-Whitney U-values for 95% confidence.
84 * For more info see http://www.saburchill.com/IBbiology/stats/003.html.
85 */
86 var uTable = {
87 '5': [0, 1, 2],
88 '6': [1, 2, 3, 5],
89 '7': [1, 3, 5, 6, 8],
90 '8': [2, 4, 6, 8, 10, 13],
91 '9': [2, 4, 7, 10, 12, 15, 17],
92 '10': [3, 5, 8, 11, 14, 17, 20, 23],
93 '11': [3, 6, 9, 13, 16, 19, 23, 26, 30],
94 '12': [4, 7, 11, 14, 18, 22, 26, 29, 33, 37],
95 '13': [4, 8, 12, 16, 20, 24, 28, 33, 37, 41, 45],
96 '14': [5, 9, 13, 17, 22, 26, 31, 36, 40, 45, 50, 55],
97 '15': [5, 10, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59, 64],
98 '16': [6, 11, 15, 21, 26, 31, 37, 42, 47, 53, 59, 64, 70, 75],
99 '17': [6, 11, 17, 22, 28, 34, 39, 45, 51, 57, 63, 67, 75, 81, 87],
100 '18': [7, 12, 18, 24, 30, 36, 42, 48, 55, 61, 67, 74, 80, 86, 93, 99],
101 '19': [7, 13, 19, 25, 32, 38, 45, 52, 58, 65, 72, 78, 85, 92, 99, 106, 113],
102 '20': [8, 14, 20, 27, 34, 41, 48, 55, 62, 69, 76, 83, 90, 98, 105, 112, 119, 127],
103 '21': [8, 15, 22, 29, 36, 43, 50, 58, 65, 73, 80, 88, 96, 103, 111, 119, 126, 134, 142],
104 '22': [9, 16, 23, 30, 38, 45, 53, 61, 69, 77, 85, 93, 101, 109, 117, 125, 133, 141, 150, 158],
105 '23': [9, 17, 24, 32, 40, 48, 56, 64, 73, 81, 89, 98, 106, 115, 123, 132, 140, 149, 157, 166, 175],
106 '24': [10, 17, 25, 33, 42, 50, 59, 67, 76, 85, 94, 102, 111, 120, 129, 138, 147, 156, 165, 174, 183, 192],
107 '25': [10, 18, 27, 35, 44, 53, 62, 71, 80, 89, 98, 107, 117, 126, 135, 145, 154, 163, 173, 182, 192, 201, 211],
108 '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],
109 '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],
110 '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],
111 '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],
112 '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]
113 };
114
115 /*--------------------------------------------------------------------------*/
116
117 /**
118 * Create a new `Benchmark` function using the given `context` object.
119 *
120 * @static
121 * @memberOf Benchmark
122 * @param {Object} [context=root] The context object.
123 * @returns {Function} Returns a new `Benchmark` function.
124 */
125 function runInContext(context) {
126 // Exit early if unable to acquire lodash.
127 var _ = context && context._ || req('lodash') || root._;
128 if (!_) {
129 Benchmark.runInContext = runInContext;
130 return Benchmark;
131 }
132 // Avoid issues with some ES3 environments that attempt to use values, named
133 // after built-in constructors like `Object`, for the creation of literals.
134 // ES5 clears this up by stating that literals must use built-in constructors.
135 // See http://es5.github.io/#x11.1.5.
136 context = context ? _.defaults(root.Object(), context, _.pick(root, contextProps)) : root;
137
138 /** Native constructor references. */
139 var Array = context.Array,
140 Date = context.Date,
141 Function = context.Function,
142 Math = context.Math,
143 Object = context.Object,
144 RegExp = context.RegExp,
145 String = context.String;
146
147 /** Used for `Array` and `Object` method references. */
148 var arrayRef = [],
149 objectProto = Object.prototype;
150
151 /** Native method shortcuts. */
152 var abs = Math.abs,
153 clearTimeout = context.clearTimeout,
154 floor = Math.floor,
155 log = Math.log,
156 max = Math.max,
157 min = Math.min,
158 pow = Math.pow,
159 push = arrayRef.push,
160 setTimeout = context.setTimeout,
161 shift = arrayRef.shift,
162 slice = arrayRef.slice,
163 sqrt = Math.sqrt,
164 toString = objectProto.toString,
165 unshift = arrayRef.unshift;
166
167 /** Detect DOM document object. */
168 var doc = isHostType(context, 'document') && context.document;
169
170 /** Used to access Wade Simmons' Node.js `microtime` module. */
171 var microtimeObject = req('microtime');
172
173 /** Used to access Node.js's high resolution timer. */
174 var processObject = isHostType(context, 'process') && context.process;
175
176 /** Used to prevent a `removeChild` memory leak in IE < 9. */
177 var trash = doc && doc.createElement('div');
178
179 /** Used to integrity check compiled tests. */
180 var uid = 'uid' + _.now();
181
182 /** Used to avoid infinite recursion when methods call each other. */
183 var calledBy = {};
184
185 /**
186 * An object used to flag environments/features.
187 *
188 * @static
189 * @memberOf Benchmark
190 * @type Object
191 */
192 var support = {};
193
194 (function() {
195
196 /**
197 * Detect if running in a browser environment.
198 *
199 * @memberOf Benchmark.support
200 * @type boolean
201 */
202 support.browser = doc && isHostType(context, 'navigator') && !isHostType(context, 'phantom');
203
204 /**
205 * Detect if the Timers API exists.
206 *
207 * @memberOf Benchmark.support
208 * @type boolean
209 */
210 support.timeout = isHostType(context, 'setTimeout') && isHostType(context, 'clearTimeout');
211
212 /**
213 * Detect if function decompilation is support.
214 *
215 * @name decompilation
216 * @memberOf Benchmark.support
217 * @type boolean
218 */
219 try {
220 // Safari 2.x removes commas in object literals from `Function#toString` results.
221 // See http://webk.it/11609 for more details.
222 // Firefox 3.6 and Opera 9.25 strip grouping parentheses from `Function#toString` results.
223 // See http://bugzil.la/559438 for more details.
224 support.decompilation = Function(
225 ('return (' + (function(x) { return { 'x': '' + (1 + x) + '', 'y': 0 }; }) + ')')
226 // Avoid issues with code added by Istanbul.
227 .replace(/__cov__[^;]+;/g, '')
228 )()(0).x === '1';
229 } catch(e) {
230 support.decompilation = false;
231 }
232 }());
233
234 /**
235 * Timer object used by `clock()` and `Deferred#resolve`.
236 *
237 * @private
238 * @type Object
239 */
240 var timer = {
241
242 /**
243 * The timer namespace object or constructor.
244 *
245 * @private
246 * @memberOf timer
247 * @type {Function|Object}
248 */
249 'ns': Date,
250
251 /**
252 * Starts the deferred timer.
253 *
254 * @private
255 * @memberOf timer
256 * @param {Object} deferred The deferred instance.
257 */
258 'start': null, // Lazy defined in `clock()`.
259
260 /**
261 * Stops the deferred timer.
262 *
263 * @private
264 * @memberOf timer
265 * @param {Object} deferred The deferred instance.
266 */
267 'stop': null // Lazy defined in `clock()`.
268 };
269
270 /*------------------------------------------------------------------------*/
271
272 /**
273 * The Benchmark constructor.
274 *
275 * Note: The Benchmark constructor exposes a handful of lodash methods to
276 * make working with arrays, collections, and objects easier. The lodash
277 * methods are:
278 * [`each/forEach`](https://lodash.com/docs#forEach), [`forOwn`](https://lodash.com/docs#forOwn),
279 * [`has`](https://lodash.com/docs#has), [`indexOf`](https://lodash.com/docs#indexOf),
280 * [`map`](https://lodash.com/docs#map), and [`reduce`](https://lodash.com/docs#reduce)
281 *
282 * @constructor
283 * @param {string} name A name to identify the benchmark.
284 * @param {Function|string} fn The test to benchmark.
285 * @param {Object} [options={}] Options object.
286 * @example
287 *
288 * // basic usage (the `new` operator is optional)
289 * var bench = new Benchmark(fn);
290 *
291 * // or using a name first
292 * var bench = new Benchmark('foo', fn);
293 *
294 * // or with options
295 * var bench = new Benchmark('foo', fn, {
296 *
297 * // displayed by `Benchmark#toString` if `name` is not available
298 * 'id': 'xyz',
299 *
300 * // called when the benchmark starts running
301 * 'onStart': onStart,
302 *
303 * // called after each run cycle
304 * 'onCycle': onCycle,
305 *
306 * // called when aborted
307 * 'onAbort': onAbort,
308 *
309 * // called when a test errors
310 * 'onError': onError,
311 *
312 * // called when reset
313 * 'onReset': onReset,
314 *
315 * // called when the benchmark completes running
316 * 'onComplete': onComplete,
317 *
318 * // compiled/called before the test loop
319 * 'setup': setup,
320 *
321 * // compiled/called after the test loop
322 * 'teardown': teardown
323 * });
324 *
325 * // or name and options
326 * var bench = new Benchmark('foo', {
327 *
328 * // a flag to indicate the benchmark is deferred
329 * 'defer': true,
330 *
331 * // benchmark test function
332 * 'fn': function(deferred) {
333 * // call `Deferred#resolve` when the deferred test is finished
334 * deferred.resolve();
335 * }
336 * });
337 *
338 * // or options only
339 * var bench = new Benchmark({
340 *
341 * // benchmark name
342 * 'name': 'foo',
343 *
344 * // benchmark test as a string
345 * 'fn': '[1,2,3,4].sort()'
346 * });
347 *
348 * // a test's `this` binding is set to the benchmark instance
349 * var bench = new Benchmark('foo', function() {
350 * 'My name is '.concat(this.name); // "My name is foo"
351 * });
352 */
353 function Benchmark(name, fn, options) {
354 var bench = this;
355
356 // Allow instance creation without the `new` operator.
357 if (!(bench instanceof Benchmark)) {
358 return new Benchmark(name, fn, options);
359 }
360 // Juggle arguments.
361 if (_.isPlainObject(name)) {
362 // 1 argument (options).
363 options = name;
364 }
365 else if (_.isFunction(name)) {
366 // 2 arguments (fn, options).
367 options = fn;
368 fn = name;
369 }
370 else if (_.isPlainObject(fn)) {
371 // 2 arguments (name, options).
372 options = fn;
373 fn = null;
374 bench.name = name;
375 }
376 else {
377 // 3 arguments (name, fn [, options]).
378 bench.name = name;
379 }
380 setOptions(bench, options);
381
382 bench.id || (bench.id = ++counter);
383 bench.fn == null && (bench.fn = fn);
384
385 bench.stats = cloneDeep(bench.stats);
386 bench.times = cloneDeep(bench.times);
387 }
388
389 /**
390 * The Deferred constructor.
391 *
392 * @constructor
393 * @memberOf Benchmark
394 * @param {Object} clone The cloned benchmark instance.
395 */
396 function Deferred(clone) {
397 var deferred = this;
398 if (!(deferred instanceof Deferred)) {
399 return new Deferred(clone);
400 }
401 deferred.benchmark = clone;
402 clock(deferred);
403 }
404
405 /**
406 * The Event constructor.
407 *
408 * @constructor
409 * @memberOf Benchmark
410 * @param {Object|string} type The event type.
411 */
412 function Event(type) {
413 var event = this;
414 if (type instanceof Event) {
415 return type;
416 }
417 return (event instanceof Event)
418 ? _.assign(event, { 'timeStamp': _.now() }, typeof type == 'string' ? { 'type': type } : type)
419 : new Event(type);
420 }
421
422 /**
423 * The Suite constructor.
424 *
425 * Note: Each Suite instance has a handful of wrapped lodash methods to
426 * make working with Suites easier. The wrapped lodash methods are:
427 * [`each/forEach`](https://lodash.com/docs#forEach), [`indexOf`](https://lodash.com/docs#indexOf),
428 * [`map`](https://lodash.com/docs#map), and [`reduce`](https://lodash.com/docs#reduce)
429 *
430 * @constructor
431 * @memberOf Benchmark
432 * @param {string} name A name to identify the suite.
433 * @param {Object} [options={}] Options object.
434 * @example
435 *
436 * // basic usage (the `new` operator is optional)
437 * var suite = new Benchmark.Suite;
438 *
439 * // or using a name first
440 * var suite = new Benchmark.Suite('foo');
441 *
442 * // or with options
443 * var suite = new Benchmark.Suite('foo', {
444 *
445 * // called when the suite starts running
446 * 'onStart': onStart,
447 *
448 * // called between running benchmarks
449 * 'onCycle': onCycle,
450 *
451 * // called when aborted
452 * 'onAbort': onAbort,
453 *
454 * // called when a test errors
455 * 'onError': onError,
456 *
457 * // called when reset
458 * 'onReset': onReset,
459 *
460 * // called when the suite completes running
461 * 'onComplete': onComplete
462 * });
463 */
464 function Suite(name, options) {
465 var suite = this;
466
467 // Allow instance creation without the `new` operator.
468 if (!(suite instanceof Suite)) {
469 return new Suite(name, options);
470 }
471 // Juggle arguments.
472 if (_.isPlainObject(name)) {
473 // 1 argument (options).
474 options = name;
475 } else {
476 // 2 arguments (name [, options]).
477 suite.name = name;
478 }
479 setOptions(suite, options);
480 }
481
482 /*------------------------------------------------------------------------*/
483
484 /**
485 * A specialized version of `_.cloneDeep` which only clones arrays and plain
486 * objects assigning all other values by reference.
487 *
488 * @private
489 * @param {*} value The value to clone.
490 * @returns {*} The cloned value.
491 */
492 var cloneDeep = _.partial(_.cloneDeepWith || _.cloneDeep, _, function(value) {
493 // Only clone primitives, arrays, and plain objects.
494 return (_.isObject(value) && !_.isArray(value) && !_.isPlainObject(value))
495 ? value
496 : undefined;
497 });
498
499 /**
500 * Creates a function from the given arguments string and body.
501 *
502 * @private
503 * @param {string} args The comma separated function arguments.
504 * @param {string} body The function body.
505 * @returns {Function} The new function.
506 */
507 function createFunction() {
508 // Lazy define.
509 createFunction = function(args, body) {
510 var result,
511 anchor = freeDefine ? freeDefine.amd : Benchmark,
512 prop = uid + 'createFunction';
513
514 runScript((freeDefine ? 'define.amd.' : 'Benchmark.') + prop + '=function(' + args + '){' + body + '}');
515 result = anchor[prop];
516 delete anchor[prop];
517 return result;
518 };
519 // Fix JaegerMonkey bug.
520 // For more information see http://bugzil.la/639720.
521 createFunction = support.browser && (createFunction('', 'return"' + uid + '"') || _.noop)() == uid ? createFunction : Function;
522 return createFunction.apply(null, arguments);
523 }
524
525 /**
526 * Delay the execution of a function based on the benchmark's `delay` property.
527 *
528 * @private
529 * @param {Object} bench The benchmark instance.
530 * @param {Object} fn The function to execute.
531 */
532 function delay(bench, fn) {
533 bench._timerId = _.delay(fn, bench.delay * 1e3);
534 }
535
536 /**
537 * Destroys the given element.
538 *
539 * @private
540 * @param {Element} element The element to destroy.
541 */
542 function destroyElement(element) {
543 trash.appendChild(element);
544 trash.innerHTML = '';
545 }
546
547 /**
548 * Gets the name of the first argument from a function's source.
549 *
550 * @private
551 * @param {Function} fn The function.
552 * @returns {string} The argument name.
553 */
554 function getFirstArgument(fn) {
555 return (!_.has(fn, 'toString') &&
556 (/^[\s(]*function[^(]*\(([^\s,)]+)/.exec(fn) || 0)[1]) || '';
557 }
558
559 /**
560 * Computes the arithmetic mean of a sample.
561 *
562 * @private
563 * @param {Array} sample The sample.
564 * @returns {number} The mean.
565 */
566 function getMean(sample) {
567 return (_.reduce(sample, function(sum, x) {
568 return sum + x;
569 }) / sample.length) || 0;
570 }
571
572 /**
573 * Gets the source code of a function.
574 *
575 * @private
576 * @param {Function} fn The function.
577 * @returns {string} The function's source code.
578 */
579 function getSource(fn) {
580 var result = '';
581 if (isStringable(fn)) {
582 result = String(fn);
583 } else if (support.decompilation) {
584 // Escape the `{` for Firefox 1.
585 result = _.result(/^[^{]+\{([\s\S]*)\}\s*$/.exec(fn), 1);
586 }
587 // Trim string.
588 result = (result || '').replace(/^\s+|\s+$/g, '');
589
590 // Detect strings containing only the "use strict" directive.
591 return /^(?:\/\*+[\w\W]*?\*\/|\/\/.*?[\n\r\u2028\u2029]|\s)*(["'])use strict\1;?$/.test(result)
592 ? ''
593 : result;
594 }
595
596 /**
597 * Checks if an object is of the specified class.
598 *
599 * @private
600 * @param {*} value The value to check.
601 * @param {string} name The name of the class.
602 * @returns {boolean} Returns `true` if the value is of the specified class, else `false`.
603 */
604 function isClassOf(value, name) {
605 return value != null && toString.call(value) == '[object ' + name + ']';
606 }
607
608 /**
609 * Host objects can return type values that are different from their actual
610 * data type. The objects we are concerned with usually return non-primitive
611 * types of "object", "function", or "unknown".
612 *
613 * @private
614 * @param {*} object The owner of the property.
615 * @param {string} property The property to check.
616 * @returns {boolean} Returns `true` if the property value is a non-primitive, else `false`.
617 */
618 function isHostType(object, property) {
619 if (object == null) {
620 return false;
621 }
622 var type = typeof object[property];
623 return !rePrimitive.test(type) && (type != 'object' || !!object[property]);
624 }
625
626 /**
627 * Checks if a value can be safely coerced to a string.
628 *
629 * @private
630 * @param {*} value The value to check.
631 * @returns {boolean} Returns `true` if the value can be coerced, else `false`.
632 */
633 function isStringable(value) {
634 return _.isString(value) || (_.has(value, 'toString') && _.isFunction(value.toString));
635 }
636
637 /**
638 * A wrapper around `require` to suppress `module missing` errors.
639 *
640 * @private
641 * @param {string} id The module id.
642 * @returns {*} The exported module or `null`.
643 */
644 function req(id) {
645 try {
646 var result = freeExports && freeRequire(id);
647 } catch(e) {}
648 return result || null;
649 }
650
651 /**
652 * Runs a snippet of JavaScript via script injection.
653 *
654 * @private
655 * @param {string} code The code to run.
656 */
657 function runScript(code) {
658 var anchor = freeDefine ? define.amd : Benchmark,
659 script = doc.createElement('script'),
660 sibling = doc.getElementsByTagName('script')[0],
661 parent = sibling.parentNode,
662 prop = uid + 'runScript',
663 prefix = '(' + (freeDefine ? 'define.amd.' : 'Benchmark.') + prop + '||function(){})();';
664
665 // Firefox 2.0.0.2 cannot use script injection as intended because it executes
666 // asynchronously, but that's OK because script injection is only used to avoid
667 // the previously commented JaegerMonkey bug.
668 try {
669 // Remove the inserted script *before* running the code to avoid differences
670 // in the expected script element count/order of the document.
671 script.appendChild(doc.createTextNode(prefix + code));
672 anchor[prop] = function() { destroyElement(script); };
673 } catch(e) {
674 parent = parent.cloneNode(false);
675 sibling = null;
676 script.text = code;
677 }
678 parent.insertBefore(script, sibling);
679 delete anchor[prop];
680 }
681
682 /**
683 * A helper function for setting options/event handlers.
684 *
685 * @private
686 * @param {Object} object The benchmark or suite instance.
687 * @param {Object} [options={}] Options object.
688 */
689 function setOptions(object, options) {
690 options = object.options = _.assign({}, cloneDeep(object.constructor.options), cloneDeep(options));
691
692 _.forOwn(options, function(value, key) {
693 if (value != null) {
694 // Add event listeners.
695 if (/^on[A-Z]/.test(key)) {
696 _.each(key.split(' '), function(key) {
697 object.on(key.slice(2).toLowerCase(), value);
698 });
699 } else if (!_.has(object, key)) {
700 object[key] = cloneDeep(value);
701 }
702 }
703 });
704 }
705
706 /*------------------------------------------------------------------------*/
707
708 /**
709 * Handles cycling/completing the deferred benchmark.
710 *
711 * @memberOf Benchmark.Deferred
712 */
713 function resolve() {
714 var deferred = this,
715 clone = deferred.benchmark,
716 bench = clone._original;
717
718 if (bench.aborted) {
719 // cycle() -> clone cycle/complete event -> compute()'s invoked bench.run() cycle/complete.
720 deferred.teardown();
721 clone.running = false;
722 cycle(deferred);
723 }
724 else if (++deferred.cycles < clone.count) {
725 clone.compiled.call(deferred, context, timer);
726 }
727 else {
728 timer.stop(deferred);
729 deferred.teardown();
730 delay(clone, function() { cycle(deferred); });
731 }
732 }
733
734 /*------------------------------------------------------------------------*/
735
736 /**
737 * A generic `Array#filter` like method.
738 *
739 * @static
740 * @memberOf Benchmark
741 * @param {Array} array The array to iterate over.
742 * @param {Function|string} callback The function/alias called per iteration.
743 * @returns {Array} A new array of values that passed callback filter.
744 * @example
745 *
746 * // get odd numbers
747 * Benchmark.filter([1, 2, 3, 4, 5], function(n) {
748 * return n % 2;
749 * }); // -> [1, 3, 5];
750 *
751 * // get fastest benchmarks
752 * Benchmark.filter(benches, 'fastest');
753 *
754 * // get slowest benchmarks
755 * Benchmark.filter(benches, 'slowest');
756 *
757 * // get benchmarks that completed without erroring
758 * Benchmark.filter(benches, 'successful');
759 */
760 function filter(array, callback) {
761 if (callback === 'successful') {
762 // Callback to exclude those that are errored, unrun, or have hz of Infinity.
763 callback = function(bench) {
764 return bench.cycles && _.isFinite(bench.hz);
765 };
766 }
767 else if (callback === 'fastest' || callback === 'slowest') {
768 // Get successful, sort by period + margin of error, and filter fastest/slowest.
769 var result = filter(array, 'successful').sort(function(a, b) {
770 a = a.stats; b = b.stats;
771 return (a.mean + a.moe > b.mean + b.moe ? 1 : -1) * (callback === 'fastest' ? 1 : -1);
772 });
773
774 return _.filter(result, function(bench) {
775 return result[0].compare(bench) == 0;
776 });
777 }
778 return _.filter(array, callback);
779 }
780
781 /**
782 * Converts a number to a more readable comma-separated string representation.
783 *
784 * @static
785 * @memberOf Benchmark
786 * @param {number} number The number to convert.
787 * @returns {string} The more readable string representation.
788 */
789 function formatNumber(number) {
790 number = String(number).split('.');
791 return number[0].replace(/(?=(?:\d{3})+$)(?!\b)/g, ',') +
792 (number[1] ? '.' + number[1] : '');
793 }
794
795 /**
796 * Invokes a method on all items in an array.
797 *
798 * @static
799 * @memberOf Benchmark
800 * @param {Array} benches Array of benchmarks to iterate over.
801 * @param {Object|string} name The name of the method to invoke OR options object.
802 * @param {...*} [args] Arguments to invoke the method with.
803 * @returns {Array} A new array of values returned from each method invoked.
804 * @example
805 *
806 * // invoke `reset` on all benchmarks
807 * Benchmark.invoke(benches, 'reset');
808 *
809 * // invoke `emit` with arguments
810 * Benchmark.invoke(benches, 'emit', 'complete', listener);
811 *
812 * // invoke `run(true)`, treat benchmarks as a queue, and register invoke callbacks
813 * Benchmark.invoke(benches, {
814 *
815 * // invoke the `run` method
816 * 'name': 'run',
817 *
818 * // pass a single argument
819 * 'args': true,
820 *
821 * // treat as queue, removing benchmarks from front of `benches` until empty
822 * 'queued': true,
823 *
824 * // called before any benchmarks have been invoked.
825 * 'onStart': onStart,
826 *
827 * // called between invoking benchmarks
828 * 'onCycle': onCycle,
829 *
830 * // called after all benchmarks have been invoked.
831 * 'onComplete': onComplete
832 * });
833 */
834 function invoke(benches, name) {
835 var args,
836 bench,
837 queued,
838 index = -1,
839 eventProps = { 'currentTarget': benches },
840 options = { 'onStart': _.noop, 'onCycle': _.noop, 'onComplete': _.noop },
841 result = _.toArray(benches);
842
843 /**
844 * Invokes the method of the current object and if synchronous, fetches the next.
845 */
846 function execute() {
847 var listeners,
848 async = isAsync(bench);
849
850 if (async) {
851 // Use `getNext` as the first listener.
852 bench.on('complete', getNext);
853 listeners = bench.events.complete;
854 listeners.splice(0, 0, listeners.pop());
855 }
856 // Execute method.
857 result[index] = _.isFunction(bench && bench[name]) ? bench[name].apply(bench, args) : undefined;
858 // If synchronous return `true` until finished.
859 return !async && getNext();
860 }
861
862 /**
863 * Fetches the next bench or executes `onComplete` callback.
864 */
865 function getNext(event) {
866 var cycleEvent,
867 last = bench,
868 async = isAsync(last);
869
870 if (async) {
871 last.off('complete', getNext);
872 last.emit('complete');
873 }
874 // Emit "cycle" event.
875 eventProps.type = 'cycle';
876 eventProps.target = last;
877 cycleEvent = Event(eventProps);
878 options.onCycle.call(benches, cycleEvent);
879
880 // Choose next benchmark if not exiting early.
881 if (!cycleEvent.aborted && raiseIndex() !== false) {
882 bench = queued ? benches[0] : result[index];
883 if (isAsync(bench)) {
884 delay(bench, execute);
885 }
886 else if (async) {
887 // Resume execution if previously asynchronous but now synchronous.
888 while (execute()) {}
889 }
890 else {
891 // Continue synchronous execution.
892 return true;
893 }
894 } else {
895 // Emit "complete" event.
896 eventProps.type = 'complete';
897 options.onComplete.call(benches, Event(eventProps));
898 }
899 // When used as a listener `event.aborted = true` will cancel the rest of
900 // the "complete" listeners because they were already called above and when
901 // used as part of `getNext` the `return false` will exit the execution while-loop.
902 if (event) {
903 event.aborted = true;
904 } else {
905 return false;
906 }
907 }
908
909 /**
910 * Checks if invoking `Benchmark#run` with asynchronous cycles.
911 */
912 function isAsync(object) {
913 // Avoid using `instanceof` here because of IE memory leak issues with host objects.
914 var async = args[0] && args[0].async;
915 return name == 'run' && (object instanceof Benchmark) &&
916 ((async == null ? object.options.async : async) && support.timeout || object.defer);
917 }
918
919 /**
920 * Raises `index` to the next defined index or returns `false`.
921 */
922 function raiseIndex() {
923 index++;
924
925 // If queued remove the previous bench.
926 if (queued && index > 0) {
927 shift.call(benches);
928 }
929 // If we reached the last index then return `false`.
930 return (queued ? benches.length : index < result.length)
931 ? index
932 : (index = false);
933 }
934 // Juggle arguments.
935 if (_.isString(name)) {
936 // 2 arguments (array, name).
937 args = slice.call(arguments, 2);
938 } else {
939 // 2 arguments (array, options).
940 options = _.assign(options, name);
941 name = options.name;
942 args = _.isArray(args = 'args' in options ? options.args : []) ? args : [args];
943 queued = options.queued;
944 }
945 // Start iterating over the array.
946 if (raiseIndex() !== false) {
947 // Emit "start" event.
948 bench = result[index];
949 eventProps.type = 'start';
950 eventProps.target = bench;
951 options.onStart.call(benches, Event(eventProps));
952
953 // End early if the suite was aborted in an "onStart" listener.
954 if (name == 'run' && (benches instanceof Suite) && benches.aborted) {
955 // Emit "cycle" event.
956 eventProps.type = 'cycle';
957 options.onCycle.call(benches, Event(eventProps));
958 // Emit "complete" event.
959 eventProps.type = 'complete';
960 options.onComplete.call(benches, Event(eventProps));
961 }
962 // Start method execution.
963 else {
964 if (isAsync(bench)) {
965 delay(bench, execute);
966 } else {
967 while (execute()) {}
968 }
969 }
970 }
971 return result;
972 }
973
974 /**
975 * Creates a string of joined array values or object key-value pairs.
976 *
977 * @static
978 * @memberOf Benchmark
979 * @param {Array|Object} object The object to operate on.
980 * @param {string} [separator1=','] The separator used between key-value pairs.
981 * @param {string} [separator2=': '] The separator used between keys and values.
982 * @returns {string} The joined result.
983 */
984 function join(object, separator1, separator2) {
985 var result = [],
986 length = (object = Object(object)).length,
987 arrayLike = length === length >>> 0;
988
989 separator2 || (separator2 = ': ');
990 _.each(object, function(value, key) {
991 result.push(arrayLike ? value : key + separator2 + value);
992 });
993 return result.join(separator1 || ',');
994 }
995
996 /*------------------------------------------------------------------------*/
997
998 /**
999 * Aborts all benchmarks in the suite.
1000 *
1001 * @name abort
1002 * @memberOf Benchmark.Suite
1003 * @returns {Object} The suite instance.
1004 */
1005 function abortSuite() {
1006 var event,
1007 suite = this,
1008 resetting = calledBy.resetSuite;
1009
1010 if (suite.running) {
1011 event = Event('abort');
1012 suite.emit(event);
1013 if (!event.cancelled || resetting) {
1014 // Avoid infinite recursion.
1015 calledBy.abortSuite = true;
1016 suite.reset();
1017 delete calledBy.abortSuite;
1018
1019 if (!resetting) {
1020 suite.aborted = true;
1021 invoke(suite, 'abort');
1022 }
1023 }
1024 }
1025 return suite;
1026 }
1027
1028 /**
1029 * Adds a test to the benchmark suite.
1030 *
1031 * @memberOf Benchmark.Suite
1032 * @param {string} name A name to identify the benchmark.
1033 * @param {Function|string} fn The test to benchmark.
1034 * @param {Object} [options={}] Options object.
1035 * @returns {Object} The benchmark instance.
1036 * @example
1037 *
1038 * // basic usage
1039 * suite.add(fn);
1040 *
1041 * // or using a name first
1042 * suite.add('foo', fn);
1043 *
1044 * // or with options
1045 * suite.add('foo', fn, {
1046 * 'onCycle': onCycle,
1047 * 'onComplete': onComplete
1048 * });
1049 *
1050 * // or name and options
1051 * suite.add('foo', {
1052 * 'fn': fn,
1053 * 'onCycle': onCycle,
1054 * 'onComplete': onComplete
1055 * });
1056 *
1057 * // or options only
1058 * suite.add({
1059 * 'name': 'foo',
1060 * 'fn': fn,
1061 * 'onCycle': onCycle,
1062 * 'onComplete': onComplete
1063 * });
1064 */
1065 function add(name, fn, options) {
1066 var suite = this,
1067 bench = new Benchmark(name, fn, options),
1068 event = Event({ 'type': 'add', 'target': bench });
1069
1070 if (suite.emit(event), !event.cancelled) {
1071 suite.push(bench);
1072 }
1073 return suite;
1074 }
1075
1076 /**
1077 * Creates a new suite with cloned benchmarks.
1078 *
1079 * @name clone
1080 * @memberOf Benchmark.Suite
1081 * @param {Object} options Options object to overwrite cloned options.
1082 * @returns {Object} The new suite instance.
1083 */
1084 function cloneSuite(options) {
1085 var suite = this,
1086 result = new suite.constructor(_.assign({}, suite.options, options));
1087
1088 // Copy own properties.
1089 _.forOwn(suite, function(value, key) {
1090 if (!_.has(result, key)) {
1091 result[key] = value && _.isFunction(value.clone)
1092 ? value.clone()
1093 : cloneDeep(value);
1094 }
1095 });
1096 return result;
1097 }
1098
1099 /**
1100 * An `Array#filter` like method.
1101 *
1102 * @name filter
1103 * @memberOf Benchmark.Suite
1104 * @param {Function|string} callback The function/alias called per iteration.
1105 * @returns {Object} A new suite of benchmarks that passed callback filter.
1106 */
1107 function filterSuite(callback) {
1108 var suite = this,
1109 result = new suite.constructor(suite.options);
1110
1111 result.push.apply(result, filter(suite, callback));
1112 return result;
1113 }
1114
1115 /**
1116 * Resets all benchmarks in the suite.
1117 *
1118 * @name reset
1119 * @memberOf Benchmark.Suite
1120 * @returns {Object} The suite instance.
1121 */
1122 function resetSuite() {
1123 var event,
1124 suite = this,
1125 aborting = calledBy.abortSuite;
1126
1127 if (suite.running && !aborting) {
1128 // No worries, `resetSuite()` is called within `abortSuite()`.
1129 calledBy.resetSuite = true;
1130 suite.abort();
1131 delete calledBy.resetSuite;
1132 }
1133 // Reset if the state has changed.
1134 else if ((suite.aborted || suite.running) &&
1135 (suite.emit(event = Event('reset')), !event.cancelled)) {
1136 suite.aborted = suite.running = false;
1137 if (!aborting) {
1138 invoke(suite, 'reset');
1139 }
1140 }
1141 return suite;
1142 }
1143
1144 /**
1145 * Runs the suite.
1146 *
1147 * @name run
1148 * @memberOf Benchmark.Suite
1149 * @param {Object} [options={}] Options object.
1150 * @returns {Object} The suite instance.
1151 * @example
1152 *
1153 * // basic usage
1154 * suite.run();
1155 *
1156 * // or with options
1157 * suite.run({ 'async': true, 'queued': true });
1158 */
1159 function runSuite(options) {
1160 var suite = this;
1161
1162 suite.reset();
1163 suite.running = true;
1164 options || (options = {});
1165
1166 invoke(suite, {
1167 'name': 'run',
1168 'args': options,
1169 'queued': options.queued,
1170 'onStart': function(event) {
1171 suite.emit(event);
1172 },
1173 'onCycle': function(event) {
1174 var bench = event.target;
1175 if (bench.error) {
1176 suite.emit({ 'type': 'error', 'target': bench });
1177 }
1178 suite.emit(event);
1179 event.aborted = suite.aborted;
1180 },
1181 'onComplete': function(event) {
1182 suite.running = false;
1183 suite.emit(event);
1184 }
1185 });
1186 return suite;
1187 }
1188
1189 /*------------------------------------------------------------------------*/
1190
1191 /**
1192 * Executes all registered listeners of the specified event type.
1193 *
1194 * @memberOf Benchmark, Benchmark.Suite
1195 * @param {Object|string} type The event type or object.
1196 * @param {...*} [args] Arguments to invoke the listener with.
1197 * @returns {*} Returns the return value of the last listener executed.
1198 */
1199 function emit(type) {
1200 var listeners,
1201 object = this,
1202 event = Event(type),
1203 events = object.events,
1204 args = (arguments[0] = event, arguments);
1205
1206 event.currentTarget || (event.currentTarget = object);
1207 event.target || (event.target = object);
1208 delete event.result;
1209
1210 if (events && (listeners = _.has(events, event.type) && events[event.type])) {
1211 _.each(listeners.slice(), function(listener) {
1212 if ((event.result = listener.apply(object, args)) === false) {
1213 event.cancelled = true;
1214 }
1215 return !event.aborted;
1216 });
1217 }
1218 return event.result;
1219 }
1220
1221 /**
1222 * Returns an array of event listeners for a given type that can be manipulated
1223 * to add or remove listeners.
1224 *
1225 * @memberOf Benchmark, Benchmark.Suite
1226 * @param {string} type The event type.
1227 * @returns {Array} The listeners array.
1228 */
1229 function listeners(type) {
1230 var object = this,
1231 events = object.events || (object.events = {});
1232
1233 return _.has(events, type) ? events[type] : (events[type] = []);
1234 }
1235
1236 /**
1237 * Unregisters a listener for the specified event type(s),
1238 * or unregisters all listeners for the specified event type(s),
1239 * or unregisters all listeners for all event types.
1240 *
1241 * @memberOf Benchmark, Benchmark.Suite
1242 * @param {string} [type] The event type.
1243 * @param {Function} [listener] The function to unregister.
1244 * @returns {Object} The benchmark instance.
1245 * @example
1246 *
1247 * // unregister a listener for an event type
1248 * bench.off('cycle', listener);
1249 *
1250 * // unregister a listener for multiple event types
1251 * bench.off('start cycle', listener);
1252 *
1253 * // unregister all listeners for an event type
1254 * bench.off('cycle');
1255 *
1256 * // unregister all listeners for multiple event types
1257 * bench.off('start cycle complete');
1258 *
1259 * // unregister all listeners for all event types
1260 * bench.off();
1261 */
1262 function off(type, listener) {
1263 var object = this,
1264 events = object.events;
1265
1266 if (!events) {
1267 return object;
1268 }
1269 _.each(type ? type.split(' ') : events, function(listeners, type) {
1270 var index;
1271 if (typeof listeners == 'string') {
1272 type = listeners;
1273 listeners = _.has(events, type) && events[type];
1274 }
1275 if (listeners) {
1276 if (listener) {
1277 index = _.indexOf(listeners, listener);
1278 if (index > -1) {
1279 listeners.splice(index, 1);
1280 }
1281 } else {
1282 listeners.length = 0;
1283 }
1284 }
1285 });
1286 return object;
1287 }
1288
1289 /**
1290 * Registers a listener for the specified event type(s).
1291 *
1292 * @memberOf Benchmark, Benchmark.Suite
1293 * @param {string} type The event type.
1294 * @param {Function} listener The function to register.
1295 * @returns {Object} The benchmark instance.
1296 * @example
1297 *
1298 * // register a listener for an event type
1299 * bench.on('cycle', listener);
1300 *
1301 * // register a listener for multiple event types
1302 * bench.on('start cycle', listener);
1303 */
1304 function on(type, listener) {
1305 var object = this,
1306 events = object.events || (object.events = {});
1307
1308 _.each(type.split(' '), function(type) {
1309 (_.has(events, type)
1310 ? events[type]
1311 : (events[type] = [])
1312 ).push(listener);
1313 });
1314 return object;
1315 }
1316
1317 /*------------------------------------------------------------------------*/
1318
1319 /**
1320 * Aborts the benchmark without recording times.
1321 *
1322 * @memberOf Benchmark
1323 * @returns {Object} The benchmark instance.
1324 */
1325 function abort() {
1326 var event,
1327 bench = this,
1328 resetting = calledBy.reset;
1329
1330 if (bench.running) {
1331 event = Event('abort');
1332 bench.emit(event);
1333 if (!event.cancelled || resetting) {
1334 // Avoid infinite recursion.
1335 calledBy.abort = true;
1336 bench.reset();
1337 delete calledBy.abort;
1338
1339 if (support.timeout) {
1340 clearTimeout(bench._timerId);
1341 delete bench._timerId;
1342 }
1343 if (!resetting) {
1344 bench.aborted = true;
1345 bench.running = false;
1346 }
1347 }
1348 }
1349 return bench;
1350 }
1351
1352 /**
1353 * Creates a new benchmark using the same test and options.
1354 *
1355 * @memberOf Benchmark
1356 * @param {Object} options Options object to overwrite cloned options.
1357 * @returns {Object} The new benchmark instance.
1358 * @example
1359 *
1360 * var bizarro = bench.clone({
1361 * 'name': 'doppelganger'
1362 * });
1363 */
1364 function clone(options) {
1365 var bench = this,
1366 result = new bench.constructor(_.assign({}, bench, options));
1367
1368 // Correct the `options` object.
1369 result.options = _.assign({}, cloneDeep(bench.options), cloneDeep(options));
1370
1371 // Copy own custom properties.
1372 _.forOwn(bench, function(value, key) {
1373 if (!_.has(result, key)) {
1374 result[key] = cloneDeep(value);
1375 }
1376 });
1377
1378 return result;
1379 }
1380
1381 /**
1382 * Determines if a benchmark is faster than another.
1383 *
1384 * @memberOf Benchmark
1385 * @param {Object} other The benchmark to compare.
1386 * @returns {number} Returns `-1` if slower, `1` if faster, and `0` if indeterminate.
1387 */
1388 function compare(other) {
1389 var bench = this;
1390
1391 // Exit early if comparing the same benchmark.
1392 if (bench == other) {
1393 return 0;
1394 }
1395 var critical,
1396 zStat,
1397 sample1 = bench.stats.sample,
1398 sample2 = other.stats.sample,
1399 size1 = sample1.length,
1400 size2 = sample2.length,
1401 maxSize = max(size1, size2),
1402 minSize = min(size1, size2),
1403 u1 = getU(sample1, sample2),
1404 u2 = getU(sample2, sample1),
1405 u = min(u1, u2);
1406
1407 function getScore(xA, sampleB) {
1408 return _.reduce(sampleB, function(total, xB) {
1409 return total + (xB > xA ? 0 : xB < xA ? 1 : 0.5);
1410 }, 0);
1411 }
1412
1413 function getU(sampleA, sampleB) {
1414 return _.reduce(sampleA, function(total, xA) {
1415 return total + getScore(xA, sampleB);
1416 }, 0);
1417 }
1418
1419 function getZ(u) {
1420 return (u - ((size1 * size2) / 2)) / sqrt((size1 * size2 * (size1 + size2 + 1)) / 12);
1421 }
1422 // Reject the null hypothesis the two samples come from the
1423 // same population (i.e. have the same median) if...
1424 if (size1 + size2 > 30) {
1425 // ...the z-stat is greater than 1.96 or less than -1.96
1426 // http://www.statisticslectures.com/topics/mannwhitneyu/
1427 zStat = getZ(u);
1428 return abs(zStat) > 1.96 ? (u == u1 ? 1 : -1) : 0;
1429 }
1430 // ...the U value is less than or equal the critical U value.
1431 critical = maxSize < 5 || minSize < 3 ? 0 : uTable[maxSize][minSize - 3];
1432 return u <= critical ? (u == u1 ? 1 : -1) : 0;
1433 }
1434
1435 /**
1436 * Reset properties and abort if running.
1437 *
1438 * @memberOf Benchmark
1439 * @returns {Object} The benchmark instance.
1440 */
1441 function reset() {
1442 var bench = this;
1443 if (bench.running && !calledBy.abort) {
1444 // No worries, `reset()` is called within `abort()`.
1445 calledBy.reset = true;
1446 bench.abort();
1447 delete calledBy.reset;
1448 return bench;
1449 }
1450 var event,
1451 index = 0,
1452 changes = [],
1453 queue = [];
1454
1455 // A non-recursive solution to check if properties have changed.
1456 // For more information see http://www.jslab.dk/articles/non.recursive.preorder.traversal.part4.
1457 var data = {
1458 'destination': bench,
1459 'source': _.assign({}, cloneDeep(bench.constructor.prototype), cloneDeep(bench.options))
1460 };
1461
1462 do {
1463 _.forOwn(data.source, function(value, key) {
1464 var changed,
1465 destination = data.destination,
1466 currValue = destination[key];
1467
1468 // Skip pseudo private properties like `_timerId` which could be a
1469 // Java object in environments like RingoJS.
1470 if (key.charAt(0) == '_') {
1471 return;
1472 }
1473 if (value && typeof value == 'object') {
1474 if (_.isArray(value)) {
1475 // Check if an array value has changed to a non-array value.
1476 if (!_.isArray(currValue)) {
1477 changed = currValue = [];
1478 }
1479 // Check if an array has changed its length.
1480 if (currValue.length != value.length) {
1481 changed = currValue = currValue.slice(0, value.length);
1482 currValue.length = value.length;
1483 }
1484 }
1485 // Check if an object has changed to a non-object value.
1486 else if (!currValue || typeof currValue != 'object') {
1487 changed = currValue = {};
1488 }
1489 // Register a changed object.
1490 if (changed) {
1491 changes.push({ 'destination': destination, 'key': key, 'value': currValue });
1492 }
1493 queue.push({ 'destination': currValue, 'source': value });
1494 }
1495 // Register a changed primitive.
1496 else if (value !== currValue && !(value == null || _.isFunction(value))) {
1497 changes.push({ 'destination': destination, 'key': key, 'value': value });
1498 }
1499 });
1500 }
1501 while ((data = queue[index++]));
1502
1503 // If changed emit the `reset` event and if it isn't cancelled reset the benchmark.
1504 if (changes.length && (bench.emit(event = Event('reset')), !event.cancelled)) {
1505 _.each(changes, function(data) {
1506 data.destination[data.key] = data.value;
1507 });
1508 }
1509 return bench;
1510 }
1511
1512 /**
1513 * Displays relevant benchmark information when coerced to a string.
1514 *
1515 * @name toString
1516 * @memberOf Benchmark
1517 * @returns {string} A string representation of the benchmark instance.
1518 */
1519 function toStringBench() {
1520 var bench = this,
1521 error = bench.error,
1522 hz = bench.hz,
1523 id = bench.id,
1524 stats = bench.stats,
1525 size = stats.sample.length,
1526 pm = '\xb1',
1527 result = bench.name || (_.isNaN(id) ? id : '<Test #' + id + '>');
1528
1529 if (error) {
1530 result += ': ' + join(error);
1531 } else {
1532 result += ' x ' + formatNumber(hz.toFixed(hz < 100 ? 2 : 0)) + ' ops/sec ' + pm +
1533 stats.rme.toFixed(2) + '% (' + size + ' run' + (size == 1 ? '' : 's') + ' sampled)';
1534 }
1535 return result;
1536 }
1537
1538 /*------------------------------------------------------------------------*/
1539
1540 /**
1541 * Clocks the time taken to execute a test per cycle (secs).
1542 *
1543 * @private
1544 * @param {Object} bench The benchmark instance.
1545 * @returns {number} The time taken.
1546 */
1547 function clock() {
1548 var options = Benchmark.options,
1549 templateData = {},
1550 timers = [{ 'ns': timer.ns, 'res': max(0.0015, getRes('ms')), 'unit': 'ms' }];
1551
1552 // Lazy define for hi-res timers.
1553 clock = function(clone) {
1554 var deferred;
1555
1556 if (clone instanceof Deferred) {
1557 deferred = clone;
1558 clone = deferred.benchmark;
1559 }
1560 var bench = clone._original,
1561 stringable = isStringable(bench.fn),
1562 count = bench.count = clone.count,
1563 decompilable = stringable || (support.decompilation && (clone.setup !== _.noop || clone.teardown !== _.noop)),
1564 id = bench.id,
1565 name = bench.name || (typeof id == 'number' ? '<Test #' + id + '>' : id),
1566 result = 0;
1567
1568 // Init `minTime` if needed.
1569 clone.minTime = bench.minTime || (bench.minTime = bench.options.minTime = options.minTime);
1570
1571 // Compile in setup/teardown functions and the test loop.
1572 // Create a new compiled test, instead of using the cached `bench.compiled`,
1573 // to avoid potential engine optimizations enabled over the life of the test.
1574 var funcBody = deferred
1575 ? 'var d#=this,${fnArg}=d#,m#=d#.benchmark._original,f#=m#.fn,su#=m#.setup,td#=m#.teardown;' +
1576 // When `deferred.cycles` is `0` then...
1577 'if(!d#.cycles){' +
1578 // set `deferred.fn`,
1579 'd#.fn=function(){var ${fnArg}=d#;if(typeof f#=="function"){try{${fn}\n}catch(e#){f#(d#)}}else{${fn}\n}};' +
1580 // set `deferred.teardown`,
1581 'd#.teardown=function(){d#.cycles=0;if(typeof td#=="function"){try{${teardown}\n}catch(e#){td#()}}else{${teardown}\n}};' +
1582 // execute the benchmark's `setup`,
1583 'if(typeof su#=="function"){try{${setup}\n}catch(e#){su#()}}else{${setup}\n};' +
1584 // start timer,
1585 't#.start(d#);' +
1586 // and then execute `deferred.fn` and return a dummy object.
1587 '}d#.fn();return{uid:"${uid}"}'
1588
1589 : 'var r#,s#,m#=this,f#=m#.fn,i#=m#.count,n#=t#.ns;${setup}\n${begin};' +
1590 'while(i#--){${fn}\n}${end};${teardown}\nreturn{elapsed:r#,uid:"${uid}"}';
1591
1592 var compiled = bench.compiled = clone.compiled = createCompiled(bench, decompilable, deferred, funcBody),
1593 isEmpty = !(templateData.fn || stringable);
1594
1595 try {
1596 if (isEmpty) {
1597 // Firefox may remove dead code from `Function#toString` results.
1598 // For more information see http://bugzil.la/536085.
1599 throw new Error('The test "' + name + '" is empty. This may be the result of dead code removal.');
1600 }
1601 else if (!deferred) {
1602 // Pretest to determine if compiled code exits early, usually by a
1603 // rogue `return` statement, by checking for a return object with the uid.
1604 bench.count = 1;
1605 compiled = decompilable && (compiled.call(bench, context, timer) || {}).uid == templateData.uid && compiled;
1606 bench.count = count;
1607 }
1608 } catch(e) {
1609 compiled = null;
1610 clone.error = e || new Error(String(e));
1611 bench.count = count;
1612 }
1613 // Fallback when a test exits early or errors during pretest.
1614 if (!compiled && !deferred && !isEmpty) {
1615 funcBody = (
1616 stringable || (decompilable && !clone.error)
1617 ? 'function f#(){${fn}\n}var r#,s#,m#=this,i#=m#.count'
1618 : 'var r#,s#,m#=this,f#=m#.fn,i#=m#.count'
1619 ) +
1620 ',n#=t#.ns;${setup}\n${begin};m#.f#=f#;while(i#--){m#.f#()}${end};' +
1621 'delete m#.f#;${teardown}\nreturn{elapsed:r#}';
1622
1623 compiled = createCompiled(bench, decompilable, deferred, funcBody);
1624
1625 try {
1626 // Pretest one more time to check for errors.
1627 bench.count = 1;
1628 compiled.call(bench, context, timer);
1629 bench.count = count;
1630 delete clone.error;
1631 }
1632 catch(e) {
1633 bench.count = count;
1634 if (!clone.error) {
1635 clone.error = e || new Error(String(e));
1636 }
1637 }
1638 }
1639 // If no errors run the full test loop.
1640 if (!clone.error) {
1641 compiled = bench.compiled = clone.compiled = createCompiled(bench, decompilable, deferred, funcBody);
1642 result = compiled.call(deferred || bench, context, timer).elapsed;
1643 }
1644 return result;
1645 };
1646
1647 /*----------------------------------------------------------------------*/
1648
1649 /**
1650 * Creates a compiled function from the given function `body`.
1651 */
1652 function createCompiled(bench, decompilable, deferred, body) {
1653 var fn = bench.fn,
1654 fnArg = deferred ? getFirstArgument(fn) || 'deferred' : '';
1655
1656 templateData.uid = uid + uidCounter++;
1657
1658 _.assign(templateData, {
1659 'setup': decompilable ? getSource(bench.setup) : interpolate('m#.setup()'),
1660 'fn': decompilable ? getSource(fn) : interpolate('m#.fn(' + fnArg + ')'),
1661 'fnArg': fnArg,
1662 'teardown': decompilable ? getSource(bench.teardown) : interpolate('m#.teardown()')
1663 });
1664
1665 // Use API of chosen timer.
1666 if (timer.unit == 'ns') {
1667 _.assign(templateData, {
1668 'begin': interpolate('s#=n#()'),
1669 'end': interpolate('r#=n#(s#);r#=r#[0]+(r#[1]/1e9)')
1670 });
1671 }
1672 else if (timer.unit == 'us') {
1673 if (timer.ns.stop) {
1674 _.assign(templateData, {
1675 'begin': interpolate('s#=n#.start()'),
1676 'end': interpolate('r#=n#.microseconds()/1e6')
1677 });
1678 } else {
1679 _.assign(templateData, {
1680 'begin': interpolate('s#=n#()'),
1681 'end': interpolate('r#=(n#()-s#)/1e6')
1682 });
1683 }
1684 }
1685 else if (timer.ns.now) {
1686 _.assign(templateData, {
1687 'begin': interpolate('s#=n#.now()'),
1688 'end': interpolate('r#=(n#.now()-s#)/1e3')
1689 });
1690 }
1691 else {
1692 _.assign(templateData, {
1693 'begin': interpolate('s#=new n#().getTime()'),
1694 'end': interpolate('r#=(new n#().getTime()-s#)/1e3')
1695 });
1696 }
1697 // Define `timer` methods.
1698 timer.start = createFunction(
1699 interpolate('o#'),
1700 interpolate('var n#=this.ns,${begin};o#.elapsed=0;o#.timeStamp=s#')
1701 );
1702
1703 timer.stop = createFunction(
1704 interpolate('o#'),
1705 interpolate('var n#=this.ns,s#=o#.timeStamp,${end};o#.elapsed=r#')
1706 );
1707
1708 // Create compiled test.
1709 return createFunction(
1710 interpolate('window,t#'),
1711 'var global = window, clearTimeout = global.clearTimeout, setTimeout = global.setTimeout;\n' +
1712 interpolate(body)
1713 );
1714 }
1715
1716 /**
1717 * Gets the current timer's minimum resolution (secs).
1718 */
1719 function getRes(unit) {
1720 var measured,
1721 begin,
1722 count = 30,
1723 divisor = 1e3,
1724 ns = timer.ns,
1725 sample = [];
1726
1727 // Get average smallest measurable time.
1728 while (count--) {
1729 if (unit == 'us') {
1730 divisor = 1e6;
1731 if (ns.stop) {
1732 ns.start();
1733 while (!(measured = ns.microseconds())) {}
1734 } else {
1735 begin = ns();
1736 while (!(measured = ns() - begin)) {}
1737 }
1738 }
1739 else if (unit == 'ns') {
1740 divisor = 1e9;
1741 begin = (begin = ns())[0] + (begin[1] / divisor);
1742 while (!(measured = ((measured = ns())[0] + (measured[1] / divisor)) - begin)) {}
1743 divisor = 1;
1744 }
1745 else if (ns.now) {
1746 begin = ns.now();
1747 while (!(measured = ns.now() - begin)) {}
1748 }
1749 else {
1750 begin = new ns().getTime();
1751 while (!(measured = new ns().getTime() - begin)) {}
1752 }
1753 // Check for broken timers.
1754 if (measured > 0) {
1755 sample.push(measured);
1756 } else {
1757 sample.push(Infinity);
1758 break;
1759 }
1760 }
1761 // Convert to seconds.
1762 return getMean(sample) / divisor;
1763 }
1764
1765 /**
1766 * Interpolates a given template string.
1767 */
1768 function interpolate(string) {
1769 // Replaces all occurrences of `#` with a unique number and template tokens with content.
1770 return _.template(string.replace(/\#/g, /\d+/.exec(templateData.uid)))(templateData);
1771 }
1772
1773 /*----------------------------------------------------------------------*/
1774
1775 // Detect Chrome's microsecond timer:
1776 // enable benchmarking via the --enable-benchmarking command
1777 // line switch in at least Chrome 7 to use chrome.Interval
1778 try {
1779 if ((timer.ns = new (context.chrome || context.chromium).Interval)) {
1780 timers.push({ 'ns': timer.ns, 'res': getRes('us'), 'unit': 'us' });
1781 }
1782 } catch(e) {}
1783
1784 // Detect Node.js's nanosecond resolution timer available in Node.js >= 0.8.
1785 if (processObject && typeof (timer.ns = processObject.hrtime) == 'function') {
1786 timers.push({ 'ns': timer.ns, 'res': getRes('ns'), 'unit': 'ns' });
1787 }
1788 // Detect Wade Simmons' Node.js `microtime` module.
1789 if (microtimeObject && typeof (timer.ns = microtimeObject.now) == 'function') {
1790 timers.push({ 'ns': timer.ns, 'res': getRes('us'), 'unit': 'us' });
1791 }
1792 // Pick timer with highest resolution.
1793 timer = (_.minBy || _.min)(timers, 'res');
1794
1795 // Error if there are no working timers.
1796 if (timer.res == Infinity) {
1797 throw new Error('Benchmark.js was unable to find a working timer.');
1798 }
1799 // Resolve time span required to achieve a percent uncertainty of at most 1%.
1800 // For more information see http://spiff.rit.edu/classes/phys273/uncert/uncert.html.
1801 options.minTime || (options.minTime = max(timer.res / 2 / 0.01, 0.05));
1802 return clock.apply(null, arguments);
1803 }
1804
1805 /*------------------------------------------------------------------------*/
1806
1807 /**
1808 * Computes stats on benchmark results.
1809 *
1810 * @private
1811 * @param {Object} bench The benchmark instance.
1812 * @param {Object} options The options object.
1813 */
1814 function compute(bench, options) {
1815 options || (options = {});
1816
1817 var async = options.async,
1818 elapsed = 0,
1819 initCount = bench.initCount,
1820 minSamples = bench.minSamples,
1821 queue = [],
1822 sample = bench.stats.sample;
1823
1824 /**
1825 * Adds a clone to the queue.
1826 */
1827 function enqueue() {
1828 queue.push(bench.clone({
1829 '_original': bench,
1830 'events': {
1831 'abort': [update],
1832 'cycle': [update],
1833 'error': [update],
1834 'start': [update]
1835 }
1836 }));
1837 }
1838
1839 /**
1840 * Updates the clone/original benchmarks to keep their data in sync.
1841 */
1842 function update(event) {
1843 var clone = this,
1844 type = event.type;
1845
1846 if (bench.running) {
1847 if (type == 'start') {
1848 // Note: `clone.minTime` prop is inited in `clock()`.
1849 clone.count = bench.initCount;
1850 }
1851 else {
1852 if (type == 'error') {
1853 bench.error = clone.error;
1854 }
1855 if (type == 'abort') {
1856 bench.abort();
1857 bench.emit('cycle');
1858 } else {
1859 event.currentTarget = event.target = bench;
1860 bench.emit(event);
1861 }
1862 }
1863 } else if (bench.aborted) {
1864 // Clear abort listeners to avoid triggering bench's abort/cycle again.
1865 clone.events.abort.length = 0;
1866 clone.abort();
1867 }
1868 }
1869
1870 /**
1871 * Determines if more clones should be queued or if cycling should stop.
1872 */
1873 function evaluate(event) {
1874 var critical,
1875 df,
1876 mean,
1877 moe,
1878 rme,
1879 sd,
1880 sem,
1881 variance,
1882 clone = event.target,
1883 done = bench.aborted,
1884 now = _.now(),
1885 size = sample.push(clone.times.period),
1886 maxedOut = size >= minSamples && (elapsed += now - clone.times.timeStamp) / 1e3 > bench.maxTime,
1887 times = bench.times,
1888 varOf = function(sum, x) { return sum + pow(x - mean, 2); };
1889
1890 // Exit early for aborted or unclockable tests.
1891 if (done || clone.hz == Infinity) {
1892 maxedOut = !(size = sample.length = queue.length = 0);
1893 }
1894
1895 if (!done) {
1896 // Compute the sample mean (estimate of the population mean).
1897 mean = getMean(sample);
1898 // Compute the sample variance (estimate of the population variance).
1899 variance = _.reduce(sample, varOf, 0) / (size - 1) || 0;
1900 // Compute the sample standard deviation (estimate of the population standard deviation).
1901 sd = sqrt(variance);
1902 // Compute the standard error of the mean (a.k.a. the standard deviation of the sampling distribution of the sample mean).
1903 sem = sd / sqrt(size);
1904 // Compute the degrees of freedom.
1905 df = size - 1;
1906 // Compute the critical value.
1907 critical = tTable[Math.round(df) || 1] || tTable.infinity;
1908 // Compute the margin of error.
1909 moe = sem * critical;
1910 // Compute the relative margin of error.
1911 rme = (moe / mean) * 100 || 0;
1912
1913 _.assign(bench.stats, {
1914 'deviation': sd,
1915 'mean': mean,
1916 'moe': moe,
1917 'rme': rme,
1918 'sem': sem,
1919 'variance': variance
1920 });
1921
1922 // Abort the cycle loop when the minimum sample size has been collected
1923 // and the elapsed time exceeds the maximum time allowed per benchmark.
1924 // We don't count cycle delays toward the max time because delays may be
1925 // increased by browsers that clamp timeouts for inactive tabs. For more
1926 // information see https://developer.mozilla.org/en/window.setTimeout#Inactive_tabs.
1927 if (maxedOut) {
1928 // Reset the `initCount` in case the benchmark is rerun.
1929 bench.initCount = initCount;
1930 bench.running = false;
1931 done = true;
1932 times.elapsed = (now - times.timeStamp) / 1e3;
1933 }
1934 if (bench.hz != Infinity) {
1935 bench.hz = 1 / mean;
1936 times.cycle = mean * bench.count;
1937 times.period = mean;
1938 }
1939 }
1940 // If time permits, increase sample size to reduce the margin of error.
1941 if (queue.length < 2 && !maxedOut) {
1942 enqueue();
1943 }
1944 // Abort the `invoke` cycle when done.
1945 event.aborted = done;
1946 }
1947
1948 // Init queue and begin.
1949 enqueue();
1950 invoke(queue, {
1951 'name': 'run',
1952 'args': { 'async': async },
1953 'queued': true,
1954 'onCycle': evaluate,
1955 'onComplete': function() { bench.emit('complete'); }
1956 });
1957 }
1958
1959 /*------------------------------------------------------------------------*/
1960
1961 /**
1962 * Cycles a benchmark until a run `count` can be established.
1963 *
1964 * @private
1965 * @param {Object} clone The cloned benchmark instance.
1966 * @param {Object} options The options object.
1967 */
1968 function cycle(clone, options) {
1969 options || (options = {});
1970
1971 var deferred;
1972 if (clone instanceof Deferred) {
1973 deferred = clone;
1974 clone = clone.benchmark;
1975 }
1976 var clocked,
1977 cycles,
1978 divisor,
1979 event,
1980 minTime,
1981 period,
1982 async = options.async,
1983 bench = clone._original,
1984 count = clone.count,
1985 times = clone.times;
1986
1987 // Continue, if not aborted between cycles.
1988 if (clone.running) {
1989 // `minTime` is set to `Benchmark.options.minTime` in `clock()`.
1990 cycles = ++clone.cycles;
1991 clocked = deferred ? deferred.elapsed : clock(clone);
1992 minTime = clone.minTime;
1993
1994 if (cycles > bench.cycles) {
1995 bench.cycles = cycles;
1996 }
1997 if (clone.error) {
1998 event = Event('error');
1999 event.message = clone.error;
2000 clone.emit(event);
2001 if (!event.cancelled) {
2002 clone.abort();
2003 }
2004 }
2005 }
2006 // Continue, if not errored.
2007 if (clone.running) {
2008 // Compute the time taken to complete last test cycle.
2009 bench.times.cycle = times.cycle = clocked;
2010 // Compute the seconds per operation.
2011 period = bench.times.period = times.period = clocked / count;
2012 // Compute the ops per second.
2013 bench.hz = clone.hz = 1 / period;
2014 // Avoid working our way up to this next time.
2015 bench.initCount = clone.initCount = count;
2016 // Do we need to do another cycle?
2017 clone.running = clocked < minTime;
2018
2019 if (clone.running) {
2020 // Tests may clock at `0` when `initCount` is a small number,
2021 // to avoid that we set its count to something a bit higher.
2022 if (!clocked && (divisor = divisors[clone.cycles]) != null) {
2023 count = floor(4e6 / divisor);
2024 }
2025 // Calculate how many more iterations it will take to achieve the `minTime`.
2026 if (count <= clone.count) {
2027 count += Math.ceil((minTime - clocked) / period);
2028 }
2029 clone.running = count != Infinity;
2030 }
2031 }
2032 // Should we exit early?
2033 event = Event('cycle');
2034 clone.emit(event);
2035 if (event.aborted) {
2036 clone.abort();
2037 }
2038 // Figure out what to do next.
2039 if (clone.running) {
2040 // Start a new cycle.
2041 clone.count = count;
2042 if (deferred) {
2043 clone.compiled.call(deferred, context, timer);
2044 } else if (async) {
2045 delay(clone, function() { cycle(clone, options); });
2046 } else {
2047 cycle(clone);
2048 }
2049 }
2050 else {
2051 // Fix TraceMonkey bug associated with clock fallbacks.
2052 // For more information see http://bugzil.la/509069.
2053 if (support.browser) {
2054 runScript(uid + '=1;delete ' + uid);
2055 }
2056 // We're done.
2057 clone.emit('complete');
2058 }
2059 }
2060
2061 /*------------------------------------------------------------------------*/
2062
2063 /**
2064 * Runs the benchmark.
2065 *
2066 * @memberOf Benchmark
2067 * @param {Object} [options={}] Options object.
2068 * @returns {Object} The benchmark instance.
2069 * @example
2070 *
2071 * // basic usage
2072 * bench.run();
2073 *
2074 * // or with options
2075 * bench.run({ 'async': true });
2076 */
2077 function run(options) {
2078 var bench = this,
2079 event = Event('start');
2080
2081 // Set `running` to `false` so `reset()` won't call `abort()`.
2082 bench.running = false;
2083 bench.reset();
2084 bench.running = true;
2085
2086 bench.count = bench.initCount;
2087 bench.times.timeStamp = _.now();
2088 bench.emit(event);
2089
2090 if (!event.cancelled) {
2091 options = { 'async': ((options = options && options.async) == null ? bench.async : options) && support.timeout };
2092
2093 // For clones created within `compute()`.
2094 if (bench._original) {
2095 if (bench.defer) {
2096 Deferred(bench);
2097 } else {
2098 cycle(bench, options);
2099 }
2100 }
2101 // For original benchmarks.
2102 else {
2103 compute(bench, options);
2104 }
2105 }
2106 return bench;
2107 }
2108
2109 /*------------------------------------------------------------------------*/
2110
2111 // Firefox 1 erroneously defines variable and argument names of functions on
2112 // the function itself as non-configurable properties with `undefined` values.
2113 // The bugginess continues as the `Benchmark` constructor has an argument
2114 // named `options` and Firefox 1 will not assign a value to `Benchmark.options`,
2115 // making it non-writable in the process, unless it is the first property
2116 // assigned by for-in loop of `_.assign()`.
2117 _.assign(Benchmark, {
2118
2119 /**
2120 * The default options copied by benchmark instances.
2121 *
2122 * @static
2123 * @memberOf Benchmark
2124 * @type Object
2125 */
2126 'options': {
2127
2128 /**
2129 * A flag to indicate that benchmark cycles will execute asynchronously
2130 * by default.
2131 *
2132 * @memberOf Benchmark.options
2133 * @type boolean
2134 */
2135 'async': false,
2136
2137 /**
2138 * A flag to indicate that the benchmark clock is deferred.
2139 *
2140 * @memberOf Benchmark.options
2141 * @type boolean
2142 */
2143 'defer': false,
2144
2145 /**
2146 * The delay between test cycles (secs).
2147 * @memberOf Benchmark.options
2148 * @type number
2149 */
2150 'delay': 0.005,
2151
2152 /**
2153 * Displayed by `Benchmark#toString` when a `name` is not available
2154 * (auto-generated if absent).
2155 *
2156 * @memberOf Benchmark.options
2157 * @type string
2158 */
2159 'id': undefined,
2160
2161 /**
2162 * The default number of times to execute a test on a benchmark's first cycle.
2163 *
2164 * @memberOf Benchmark.options
2165 * @type number
2166 */
2167 'initCount': 1,
2168
2169 /**
2170 * The maximum time a benchmark is allowed to run before finishing (secs).
2171 *
2172 * Note: Cycle delays aren't counted toward the maximum time.
2173 *
2174 * @memberOf Benchmark.options
2175 * @type number
2176 */
2177 'maxTime': 5,
2178
2179 /**
2180 * The minimum sample size required to perform statistical analysis.
2181 *
2182 * @memberOf Benchmark.options
2183 * @type number
2184 */
2185 'minSamples': 5,
2186
2187 /**
2188 * The time needed to reduce the percent uncertainty of measurement to 1% (secs).
2189 *
2190 * @memberOf Benchmark.options
2191 * @type number
2192 */
2193 'minTime': 0,
2194
2195 /**
2196 * The name of the benchmark.
2197 *
2198 * @memberOf Benchmark.options
2199 * @type string
2200 */
2201 'name': undefined,
2202
2203 /**
2204 * An event listener called when the benchmark is aborted.
2205 *
2206 * @memberOf Benchmark.options
2207 * @type Function
2208 */
2209 'onAbort': undefined,
2210
2211 /**
2212 * An event listener called when the benchmark completes running.
2213 *
2214 * @memberOf Benchmark.options
2215 * @type Function
2216 */
2217 'onComplete': undefined,
2218
2219 /**
2220 * An event listener called after each run cycle.
2221 *
2222 * @memberOf Benchmark.options
2223 * @type Function
2224 */
2225 'onCycle': undefined,
2226
2227 /**
2228 * An event listener called when a test errors.
2229 *
2230 * @memberOf Benchmark.options
2231 * @type Function
2232 */
2233 'onError': undefined,
2234
2235 /**
2236 * An event listener called when the benchmark is reset.
2237 *
2238 * @memberOf Benchmark.options
2239 * @type Function
2240 */
2241 'onReset': undefined,
2242
2243 /**
2244 * An event listener called when the benchmark starts running.
2245 *
2246 * @memberOf Benchmark.options
2247 * @type Function
2248 */
2249 'onStart': undefined
2250 },
2251
2252 /**
2253 * Platform object with properties describing things like browser name,
2254 * version, and operating system. See [`platform.js`](http://mths.be/platform).
2255 *
2256 * @static
2257 * @memberOf Benchmark
2258 * @type Object
2259 */
2260 'platform': context.platform || req('platform') || ({
2261 'description': context.navigator && context.navigator.userAgent || null,
2262 'layout': null,
2263 'product': null,
2264 'name': null,
2265 'manufacturer': null,
2266 'os': null,
2267 'prerelease': null,
2268 'version': null,
2269 'toString': function() {
2270 return this.description || '';
2271 }
2272 }),
2273
2274 /**
2275 * The semantic version number.
2276 *
2277 * @static
2278 * @memberOf Benchmark
2279 * @type string
2280 */
2281 'version': '2.0.0'
2282 });
2283
2284 _.assign(Benchmark, {
2285 'filter': filter,
2286 'formatNumber': formatNumber,
2287 'invoke': invoke,
2288 'join': join,
2289 'runInContext': runInContext,
2290 'support': support
2291 });
2292
2293 // Add lodash methods to Benchmark.
2294 _.each(['each', 'forEach', 'forOwn', 'has', 'indexOf', 'map', 'reduce'], function(methodName) {
2295 Benchmark[methodName] = _[methodName];
2296 });
2297
2298 /*------------------------------------------------------------------------*/
2299
2300 _.assign(Benchmark.prototype, {
2301
2302 /**
2303 * The number of times a test was executed.
2304 *
2305 * @memberOf Benchmark
2306 * @type number
2307 */
2308 'count': 0,
2309
2310 /**
2311 * The number of cycles performed while benchmarking.
2312 *
2313 * @memberOf Benchmark
2314 * @type number
2315 */
2316 'cycles': 0,
2317
2318 /**
2319 * The number of executions per second.
2320 *
2321 * @memberOf Benchmark
2322 * @type number
2323 */
2324 'hz': 0,
2325
2326 /**
2327 * The compiled test function.
2328 *
2329 * @memberOf Benchmark
2330 * @type {Function|string}
2331 */
2332 'compiled': undefined,
2333
2334 /**
2335 * The error object if the test failed.
2336 *
2337 * @memberOf Benchmark
2338 * @type Object
2339 */
2340 'error': undefined,
2341
2342 /**
2343 * The test to benchmark.
2344 *
2345 * @memberOf Benchmark
2346 * @type {Function|string}
2347 */
2348 'fn': undefined,
2349
2350 /**
2351 * A flag to indicate if the benchmark is aborted.
2352 *
2353 * @memberOf Benchmark
2354 * @type boolean
2355 */
2356 'aborted': false,
2357
2358 /**
2359 * A flag to indicate if the benchmark is running.
2360 *
2361 * @memberOf Benchmark
2362 * @type boolean
2363 */
2364 'running': false,
2365
2366 /**
2367 * Compiled into the test and executed immediately **before** the test loop.
2368 *
2369 * @memberOf Benchmark
2370 * @type {Function|string}
2371 * @example
2372 *
2373 * // basic usage
2374 * var bench = Benchmark({
2375 * 'setup': function() {
2376 * var c = this.count,
2377 * element = document.getElementById('container');
2378 * while (c--) {
2379 * element.appendChild(document.createElement('div'));
2380 * }
2381 * },
2382 * 'fn': function() {
2383 * element.removeChild(element.lastChild);
2384 * }
2385 * });
2386 *
2387 * // compiles to something like:
2388 * var c = this.count,
2389 * element = document.getElementById('container');
2390 * while (c--) {
2391 * element.appendChild(document.createElement('div'));
2392 * }
2393 * var start = new Date;
2394 * while (count--) {
2395 * element.removeChild(element.lastChild);
2396 * }
2397 * var end = new Date - start;
2398 *
2399 * // or using strings
2400 * var bench = Benchmark({
2401 * 'setup': '\
2402 * var a = 0;\n\
2403 * (function() {\n\
2404 * (function() {\n\
2405 * (function() {',
2406 * 'fn': 'a += 1;',
2407 * 'teardown': '\
2408 * }())\n\
2409 * }())\n\
2410 * }())'
2411 * });
2412 *
2413 * // compiles to something like:
2414 * var a = 0;
2415 * (function() {
2416 * (function() {
2417 * (function() {
2418 * var start = new Date;
2419 * while (count--) {
2420 * a += 1;
2421 * }
2422 * var end = new Date - start;
2423 * }())
2424 * }())
2425 * }())
2426 */
2427 'setup': _.noop,
2428
2429 /**
2430 * Compiled into the test and executed immediately **after** the test loop.
2431 *
2432 * @memberOf Benchmark
2433 * @type {Function|string}
2434 */
2435 'teardown': _.noop,
2436
2437 /**
2438 * An object of stats including mean, margin or error, and standard deviation.
2439 *
2440 * @memberOf Benchmark
2441 * @type Object
2442 */
2443 'stats': {
2444
2445 /**
2446 * The margin of error.
2447 *
2448 * @memberOf Benchmark#stats
2449 * @type number
2450 */
2451 'moe': 0,
2452
2453 /**
2454 * The relative margin of error (expressed as a percentage of the mean).
2455 *
2456 * @memberOf Benchmark#stats
2457 * @type number
2458 */
2459 'rme': 0,
2460
2461 /**
2462 * The standard error of the mean.
2463 *
2464 * @memberOf Benchmark#stats
2465 * @type number
2466 */
2467 'sem': 0,
2468
2469 /**
2470 * The sample standard deviation.
2471 *
2472 * @memberOf Benchmark#stats
2473 * @type number
2474 */
2475 'deviation': 0,
2476
2477 /**
2478 * The sample arithmetic mean (secs).
2479 *
2480 * @memberOf Benchmark#stats
2481 * @type number
2482 */
2483 'mean': 0,
2484
2485 /**
2486 * The array of sampled periods.
2487 *
2488 * @memberOf Benchmark#stats
2489 * @type Array
2490 */
2491 'sample': [],
2492
2493 /**
2494 * The sample variance.
2495 *
2496 * @memberOf Benchmark#stats
2497 * @type number
2498 */
2499 'variance': 0
2500 },
2501
2502 /**
2503 * An object of timing data including cycle, elapsed, period, start, and stop.
2504 *
2505 * @memberOf Benchmark
2506 * @type Object
2507 */
2508 'times': {
2509
2510 /**
2511 * The time taken to complete the last cycle (secs).
2512 *
2513 * @memberOf Benchmark#times
2514 * @type number
2515 */
2516 'cycle': 0,
2517
2518 /**
2519 * The time taken to complete the benchmark (secs).
2520 *
2521 * @memberOf Benchmark#times
2522 * @type number
2523 */
2524 'elapsed': 0,
2525
2526 /**
2527 * The time taken to execute the test once (secs).
2528 *
2529 * @memberOf Benchmark#times
2530 * @type number
2531 */
2532 'period': 0,
2533
2534 /**
2535 * A timestamp of when the benchmark started (ms).
2536 *
2537 * @memberOf Benchmark#times
2538 * @type number
2539 */
2540 'timeStamp': 0
2541 }
2542 });
2543
2544 _.assign(Benchmark.prototype, {
2545 'abort': abort,
2546 'clone': clone,
2547 'compare': compare,
2548 'emit': emit,
2549 'listeners': listeners,
2550 'off': off,
2551 'on': on,
2552 'reset': reset,
2553 'run': run,
2554 'toString': toStringBench
2555 });
2556
2557 /*------------------------------------------------------------------------*/
2558
2559 _.assign(Deferred.prototype, {
2560
2561 /**
2562 * The deferred benchmark instance.
2563 *
2564 * @memberOf Benchmark.Deferred
2565 * @type Object
2566 */
2567 'benchmark': null,
2568
2569 /**
2570 * The number of deferred cycles performed while benchmarking.
2571 *
2572 * @memberOf Benchmark.Deferred
2573 * @type number
2574 */
2575 'cycles': 0,
2576
2577 /**
2578 * The time taken to complete the deferred benchmark (secs).
2579 *
2580 * @memberOf Benchmark.Deferred
2581 * @type number
2582 */
2583 'elapsed': 0,
2584
2585 /**
2586 * A timestamp of when the deferred benchmark started (ms).
2587 *
2588 * @memberOf Benchmark.Deferred
2589 * @type number
2590 */
2591 'timeStamp': 0
2592 });
2593
2594 _.assign(Deferred.prototype, {
2595 'resolve': resolve
2596 });
2597
2598 /*------------------------------------------------------------------------*/
2599
2600 _.assign(Event.prototype, {
2601
2602 /**
2603 * A flag to indicate if the emitters listener iteration is aborted.
2604 *
2605 * @memberOf Benchmark.Event
2606 * @type boolean
2607 */
2608 'aborted': false,
2609
2610 /**
2611 * A flag to indicate if the default action is cancelled.
2612 *
2613 * @memberOf Benchmark.Event
2614 * @type boolean
2615 */
2616 'cancelled': false,
2617
2618 /**
2619 * The object whose listeners are currently being processed.
2620 *
2621 * @memberOf Benchmark.Event
2622 * @type Object
2623 */
2624 'currentTarget': undefined,
2625
2626 /**
2627 * The return value of the last executed listener.
2628 *
2629 * @memberOf Benchmark.Event
2630 * @type Mixed
2631 */
2632 'result': undefined,
2633
2634 /**
2635 * The object to which the event was originally emitted.
2636 *
2637 * @memberOf Benchmark.Event
2638 * @type Object
2639 */
2640 'target': undefined,
2641
2642 /**
2643 * A timestamp of when the event was created (ms).
2644 *
2645 * @memberOf Benchmark.Event
2646 * @type number
2647 */
2648 'timeStamp': 0,
2649
2650 /**
2651 * The event type.
2652 *
2653 * @memberOf Benchmark.Event
2654 * @type string
2655 */
2656 'type': ''
2657 });
2658
2659 /*------------------------------------------------------------------------*/
2660
2661 /**
2662 * The default options copied by suite instances.
2663 *
2664 * @static
2665 * @memberOf Benchmark.Suite
2666 * @type Object
2667 */
2668 Suite.options = {
2669
2670 /**
2671 * The name of the suite.
2672 *
2673 * @memberOf Benchmark.Suite.options
2674 * @type string
2675 */
2676 'name': undefined
2677 };
2678
2679 /*------------------------------------------------------------------------*/
2680
2681 _.assign(Suite.prototype, {
2682
2683 /**
2684 * The number of benchmarks in the suite.
2685 *
2686 * @memberOf Benchmark.Suite
2687 * @type number
2688 */
2689 'length': 0,
2690
2691 /**
2692 * A flag to indicate if the suite is aborted.
2693 *
2694 * @memberOf Benchmark.Suite
2695 * @type boolean
2696 */
2697 'aborted': false,
2698
2699 /**
2700 * A flag to indicate if the suite is running.
2701 *
2702 * @memberOf Benchmark.Suite
2703 * @type boolean
2704 */
2705 'running': false
2706 });
2707
2708 _.assign(Suite.prototype, {
2709 'abort': abortSuite,
2710 'add': add,
2711 'clone': cloneSuite,
2712 'emit': emit,
2713 'filter': filterSuite,
2714 'join': arrayRef.join,
2715 'listeners': listeners,
2716 'off': off,
2717 'on': on,
2718 'pop': arrayRef.pop,
2719 'push': push,
2720 'reset': resetSuite,
2721 'run': runSuite,
2722 'reverse': arrayRef.reverse,
2723 'shift': shift,
2724 'slice': slice,
2725 'sort': arrayRef.sort,
2726 'splice': arrayRef.splice,
2727 'unshift': unshift
2728 });
2729
2730 /*------------------------------------------------------------------------*/
2731
2732 // Expose Deferred, Event, and Suite.
2733 _.assign(Benchmark, {
2734 'Deferred': Deferred,
2735 'Event': Event,
2736 'Suite': Suite
2737 });
2738
2739 /*------------------------------------------------------------------------*/
2740
2741 // Add lodash methods as Suite methods.
2742 _.each(['each', 'forEach', 'indexOf', 'map', 'reduce'], function(methodName) {
2743 var func = _[methodName];
2744 Suite.prototype[methodName] = function() {
2745 var args = [this];
2746 push.apply(args, arguments);
2747 return func.apply(_, args);
2748 };
2749 });
2750
2751 // Avoid array-like object bugs with `Array#shift` and `Array#splice`
2752 // in Firefox < 10 and IE < 9.
2753 _.each(['pop', 'shift', 'splice'], function(methodName) {
2754 var func = arrayRef[methodName];
2755
2756 Suite.prototype[methodName] = function() {
2757 var value = this,
2758 result = func.apply(value, arguments);
2759
2760 if (value.length === 0) {
2761 delete value[0];
2762 }
2763 return result;
2764 };
2765 });
2766
2767 // Avoid buggy `Array#unshift` in IE < 8 which doesn't return the new
2768 // length of the array.
2769 Suite.prototype.unshift = function() {
2770 var value = this;
2771 unshift.apply(value, arguments);
2772 return value.length;
2773 };
2774
2775 return Benchmark;
2776 }
2777
2778 /*--------------------------------------------------------------------------*/
2779
2780 // Export Benchmark.
2781 // Some AMD build optimizers, like r.js, check for condition patterns like the following:
2782 if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
2783 // Define as an anonymous module so, through path mapping, it can be aliased.
2784 define(['lodash', 'platform'], function(_, platform) {
2785 return runInContext({
2786 '_': _,
2787 'platform': platform
2788 });
2789 });
2790 }
2791 else {
2792 var Benchmark = runInContext();
2793
2794 // Check for `exports` after `define` in case a build optimizer adds an `exports` object.
2795 if (freeExports && freeModule) {
2796 // Export for Node.js.
2797 if (moduleExports) {
2798 (freeModule.exports = Benchmark).Benchmark = Benchmark;
2799 }
2800 // Export for CommonJS support.
2801 freeExports.Benchmark = Benchmark;
2802 }
2803 else {
2804 // Export to the global object.
2805 root.Benchmark = Benchmark;
2806 }
2807 }
2808}.call(this));