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