Browse files

1.7.3: Use the more precise performance.now() where available.

  • Loading branch information...
1 parent 25e5e3d commit dfd723fdee9b7a73fb374f8c2ae58966643a2727 @ryanve committed Dec 17, 2013
Showing with 19 additions and 13 deletions.
  1. +8 −5 aok.js
  2. +2 −2 aok.min.js
  3. +2 −2 package.json
  4. +7 −4 src/index.js
View
13 aok.js
@@ -1,5 +1,5 @@
/*!
- * aok 1.7.2+201311230458
+ * aok 1.7.3+201312170458
* https://github.com/ryanve/aok
* MIT License 2013 Ryan Van Etten
*/
@@ -17,6 +17,7 @@
, win = typeof window != 'undefined' && window
, nativeConsole = typeof console != 'undefined' && console
, hasAlert = win && 'alert' in win
+ , performance = win['performance']
, uid = 0
, has = function(o, k) {
return owns.call(o, k);
@@ -178,11 +179,13 @@
* @param {Function} fn
* @return {number} millisecond time for `fn` to run `trials` times
*/
- aok['perform'] = function(trials, fn) {
- var i = 0, time = +new Date;
+ function perform(trials, fn) {
+ var i = 0, precise = perform['precise'], time = precise ? performance.now() : +new Date;
while (i++ < trials) fn.call(this);
- return +new Date - time;
- };
+ return (precise ? performance.now() : +new Date)-time;
+ }
+ perform['precise'] = !!performance && 'now' in performance;
+ aok['perform'] = perform;
/**
* @this {*} scope to run in
View
4 aok.min.js
@@ -1,6 +1,6 @@
/*!
- * aok 1.7.2+201311230458
+ * aok 1.7.3+201312170458
* https://github.com/ryanve/aok
* MIT License 2013 Ryan Van Etten
*/
-!function(a,b,c){"undefined"!=typeof module&&module.exports?module.exports=c():a[b]=c()}(this,"aok",function(){function a(a){"object"==typeof a&&a?m(this,a):arguments.length&&(this.test=a),this.init()}function b(b){return arguments.length?new a(b):new a}var c=b.prototype=a.prototype,d=this,e={},f=e.hasOwnProperty,g=e.toString,h="undefined"!=typeof window&&window,i="undefined"!=typeof console&&console,j=h&&"alert"in h,k=0,l=function(a,b){return f.call(a,b)},m=function(a,b){for(var c in b)l(b,c)&&(a[c]=b[c]);return a},n=function(a){return m(function(){a.apply(this,arguments)},a)};return m(b,b.console=function(a,b,c,d){function e(e,f,g){var h=b&&"function"==typeof b[e]?function(){b[e].apply(b,arguments)}:g?g:c?function(){h.force&&d.alert(e+": "+[].join.call(arguments," "))}:function(){};h.force=!!f,a[e]=h}return e("log"),e("trace"),e("info",1),e("warn",1),e("error",1),e("clear",0,function(){}),e("assert",1,function(b,c){b||a.warn(c)}),a}({},i,j,h)),c.express=b.express=n(b.log),c.pass="Pass",c.fail="Fail",c.init=function(){if(this===d)throw new Error("@this");return l(this,"id")||(this.id=++k),l(this,"test")&&this.run(),this},c.run=function(){if(this===d)throw new Error("@this");return this.test=!!b.result(this,"test"),this.handler()},c.cull=function(a){return this[this[null==a?"test":a]?"pass":"fail"]},c.handler=function(){var a=this.cull();return"function"==typeof a?a.call(this):this.express("#"+this.id+": "+this.explain(a)),this},c.explain=b.explain=function(a){return a=arguments.length?a:this,a===Object(a)?g.call(a):""+a},b.result=function(a,b,c){return c||b===c?(b=a,a=this):"function"==typeof b?b:b=a[b],"function"==typeof b?b.call(a):b},b.pass=function(a,b,c,d){if("number"==typeof b)return a?1:0;for(var e=a.length,f=0,g=0;e>f&&(!b.call(c,a[f],f++,a)||++g!==d););return g},b.fail=function(a,b,c,d){if("number"==typeof b)return a?0:1;for(var e=a.length,f=0,g=0;e>f&&(b.call(c,a[f],f++,a)||++g!==d););return g},b.perform=function(a,b){for(var c=0,d=+new Date;c++<a;)b.call(this);return+new Date-d},b.race=function(a,c){c=[].concat(c);for(var d=b.perform,e=c.length,f=0;e>f;)c[f]=d.call(this,a,c[f++]);return c},b.can=function(a){return function(){try{("string"==typeof a?this[a]:a).apply(this,arguments)}catch(b){return!1}return!0}},b.id=function(a){return document.getElementById(a)||!1},b});
+!function(a,b,c){"undefined"!=typeof module&&module.exports?module.exports=c():a[b]=c()}(this,"aok",function(){function a(a){"object"==typeof a&&a?o(this,a):arguments.length&&(this.test=a),this.init()}function b(b){return arguments.length?new a(b):new a}function c(a,b){for(var d=0,e=c.precise,f=e?l.now():+new Date;d++<a;)b.call(this);return(e?l.now():+new Date)-f}var d=b.prototype=a.prototype,e=this,f={},g=f.hasOwnProperty,h=f.toString,i="undefined"!=typeof window&&window,j="undefined"!=typeof console&&console,k=i&&"alert"in i,l=i.performance,m=0,n=function(a,b){return g.call(a,b)},o=function(a,b){for(var c in b)n(b,c)&&(a[c]=b[c]);return a},p=function(a){return o(function(){a.apply(this,arguments)},a)};return o(b,b.console=function(a,b,c,d){function e(e,f,g){var h=b&&"function"==typeof b[e]?function(){b[e].apply(b,arguments)}:g?g:c?function(){h.force&&d.alert(e+": "+[].join.call(arguments," "))}:function(){};h.force=!!f,a[e]=h}return e("log"),e("trace"),e("info",1),e("warn",1),e("error",1),e("clear",0,function(){}),e("assert",1,function(b,c){b||a.warn(c)}),a}({},j,k,i)),d.express=b.express=p(b.log),d.pass="Pass",d.fail="Fail",d.init=function(){if(this===e)throw new Error("@this");return n(this,"id")||(this.id=++m),n(this,"test")&&this.run(),this},d.run=function(){if(this===e)throw new Error("@this");return this.test=!!b.result(this,"test"),this.handler()},d.cull=function(a){return this[this[null==a?"test":a]?"pass":"fail"]},d.handler=function(){var a=this.cull();return"function"==typeof a?a.call(this):this.express("#"+this.id+": "+this.explain(a)),this},d.explain=b.explain=function(a){return a=arguments.length?a:this,a===Object(a)?h.call(a):""+a},b.result=function(a,b,c){return c||b===c?(b=a,a=this):"function"==typeof b?b:b=a[b],"function"==typeof b?b.call(a):b},b.pass=function(a,b,c,d){if("number"==typeof b)return a?1:0;for(var e=a.length,f=0,g=0;e>f&&(!b.call(c,a[f],f++,a)||++g!==d););return g},b.fail=function(a,b,c,d){if("number"==typeof b)return a?0:1;for(var e=a.length,f=0,g=0;e>f&&(b.call(c,a[f],f++,a)||++g!==d););return g},c.precise=!!l&&"now"in l,b.perform=c,b.race=function(a,c){c=[].concat(c);for(var d=b.perform,e=c.length,f=0;e>f;)c[f]=d.call(this,a,c[f++]);return c},b.can=function(a){return function(){try{("string"==typeof a?this[a]:a).apply(this,arguments)}catch(b){return!1}return!0}},b.id=function(a){return document.getElementById(a)||!1},b});
View
4 package.json
@@ -1,11 +1,11 @@
{
"name": "aok",
"description": "Extensible test suite API.",
- "version": "1.7.2",
+ "version": "1.7.3",
"homepage": "https://github.com/ryanve/aok",
"license": "MIT",
"author": "Ryan Van Etten",
- "keywords": ["testing", "test suite", "javascript", "ender", "browser", "server"],
+ "keywords": ["testing", "test suite", "performance", "javascript", "ender", "browser", "server"],
"main": "./aok.js",
"ender": "noop",
"repository": {
View
11 src/index.js
@@ -11,6 +11,7 @@
, win = typeof window != 'undefined' && window
, nativeConsole = typeof console != 'undefined' && console
, hasAlert = win && 'alert' in win
+ , performance = win['performance']
, uid = 0
, has = function(o, k) {
return owns.call(o, k);
@@ -172,11 +173,13 @@
* @param {Function} fn
* @return {number} millisecond time for `fn` to run `trials` times
*/
- aok['perform'] = function(trials, fn) {
- var i = 0, time = +new Date;
+ function perform(trials, fn) {
+ var i = 0, precise = perform['precise'], time = precise ? performance.now() : +new Date;
while (i++ < trials) fn.call(this);
- return +new Date - time;
- };
+ return (precise ? performance.now() : +new Date)-time;
+ }
+ perform['precise'] = !!performance && 'now' in performance;
+ aok['perform'] = perform;
/**
* @this {*} scope to run in

0 comments on commit dfd723f

Please sign in to comment.