├── .gitignore
├── README.md
├── bower.json
├── demo.html
├── demo.js
├── lib
└── timely.js
├── license.txt
├── package.json
└── test
└── test-timely.js
/.gitignore:
--------------------------------------------------------------------------------
1 | *~
2 | *.swp
3 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Timely - Lightweight timing of JavaScript functions
2 |
3 | When developing in JavaScript you often find yourself in situations where you want to measure the time it takes for a function to execute. Profilers can find this out but if you just want to measure the time for a function or two you might want to try Timely. Timely doesn't affect the parameters or return values of the functions that it measures. Instead it *decorates* the existing functions with a timing functionality that is completely transparent to the callers.
4 |
5 | ## Installation
6 |
7 | Node
8 |
9 | ```bash
10 | npm install timely
11 | ```
12 |
13 | Browser
14 |
15 | ```bash
16 | bower install timely
17 | ```
18 |
19 | ## Usage (in Node)
20 |
21 | ```js
22 | var timely = require('timely'),
23 |
24 | // Synchronous function (Fibonacci)
25 | fib = function(n) {
26 | if (n <= 2) return n;
27 | return fib(n - 1) + fib(n - 2);
28 | },
29 |
30 | // Asynchronous function with callback
31 | wait = function(n, cb) {
32 | setTimeout(function() {
33 | cb(n);
34 | }, 100);
35 | },
36 |
37 | // Asynchronous function returning a promise
38 | wait2 = function(n) {
39 | var defer = when.defer();
40 | setTimeout(function() {
41 | defer.resolve(n);
42 | }, 100);
43 | return defer.promise;
44 | },
45 |
46 | // Create a timed verion of the Synchronous funciton
47 | fibT = timely(fib),
48 | // Create a timed verion of the Asynchronous function with callback
49 | waitT = timely.async(wait),
50 | // Create a timed verion of the Asynchronous function returning a promise
51 | waitT2 = timely.promise(wait2),
52 |
53 | // Result of Synchronous function
54 | resultSync;
55 |
56 | // Call Synchronous function
57 | resultSync = fibT(35);
58 | //Output results of Synchronous function. fibT.time contains the time used for the last call
59 | console.log('fib(35) = ' + resultSync + ', time: ' + fibT.time + 'ms');
60 |
61 | // Call Asynchronous function with callback
62 | waitT(42, function(resultAsync) {
63 | //Output results of Asynchronous function. waitT.time contains the time used for the last call
64 | console.log('wait(42) = ' + resultAsync + ', time: ' + waitT.time + 'ms');
65 | });
66 |
67 | // Call Asynchronous function returning a promise
68 | waitT2(42).then(function(resultAsync) {
69 | //Output results of Asynchronous function. waitT2.time contains the time used for the last call
70 | console.log('wait2(42) = ' + resultAsync + ', time: ' + waitT2.time + 'ms');
71 | });
72 | ```
73 | ### Outputs:
74 |
75 | ```js
76 | fib(35) = 14930352, time: 201ms
77 | wait(42) = 42, time: 101ms
78 | wait2(42) = 42, time: 102ms
79 | ```
80 |
81 | ## Demo
82 | To run Node demo:
83 |
84 | ```bash
85 | node demo.js
86 | ```
87 |
88 | To run Browser demo:
89 |
90 | ```bash
91 | Open demo.html in your favorite browser
92 | ```
93 |
94 | ## API
95 | * [timely](#timely)
96 | * [timely.async](#timely.async)
97 | * [timely.promise](#timely.promise)
98 |
99 | ## Functions
100 |
101 |
102 | ### timely( func )
103 |
104 | Returns a decorated version of a synchronous function func that measures its execution time. After the function has returned the measured time is available in the returned function's property "time". The mean time of all calls is founcd in the property "meanTime".
105 |
106 | __Arguments__
107 |
108 | func {Function} Function to be timed
109 |
110 | ---------------------------------------
111 |
112 |
113 | ### timely.async( func )
114 |
115 | Returns a decorated version of an asynchronous function func that measures its execution time. After the callback has been called the measured time is available in the returned function's property "time". It is assumed that the callback is the last parameter of the function func. The mean time of all calls is founcd in the property "meanTime".
116 |
117 | __Arguments__
118 |
119 | func {Function} Function to be timed
120 |
121 | ---------------------------------------
122 |
123 |
124 | ### timely.promise( func )
125 |
126 | Returns a decorated version of an asynchronous function func that measures its execution time. After the returned promise has been resolved the measured time is available in the returned function's property "time". The mean time of all calls is founcd in the property "meanTime".
127 |
128 | __Arguments__
129 |
130 | func {Function} Function to be timed
131 |
132 | ---------------------------------------
133 |
134 | ## Tests
135 |
136 | ```bash
137 | npm install
138 | npm test
139 | ```
140 |
141 | ## License
142 | (MIT License)
143 |
144 | Copyright (c) 2014 Aron Kornhall
145 |
146 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
147 |
148 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
149 |
150 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
151 |
152 |
--------------------------------------------------------------------------------
/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "timely",
3 | "version": "0.1.0",
4 | "authors": [
5 | "Aron Kornhall "
6 | ],
7 | "description": "Lightweight and easy to use timing decorators",
8 | "keywords": [
9 | "timing",
10 | "measure time"
11 | ],
12 | "main": "lib/timely.js",
13 | "moduleType": [
14 | "amd",
15 | "globals",
16 | "node"
17 | ],
18 | "license": "MIT",
19 | "ignore": [
20 | "**/.*",
21 | "node_modules",
22 | "bower_components",
23 | "test",
24 | "tests"
25 | ]
26 | }
27 |
--------------------------------------------------------------------------------
/demo.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Demo of Timely in Browser
4 |
5 |
6 |
7 |
8 |
9 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/demo.js:
--------------------------------------------------------------------------------
1 | // Demo of timely
2 | // Run with:
3 | // >node demo.js
4 | // Output:
5 | // >fib(35) = 14930352, time: 201ms
6 | // >wait(42) = 42, time: 101ms
7 |
8 | var timely = require('./lib/timely'),
9 | when = require('when'),
10 |
11 | // Synchronous function (Fibonacci)
12 | fib = function(n) {
13 | if (n <= 2) return n;
14 | return fib(n - 1) + fib(n - 2);
15 | },
16 |
17 | // Asynchronous function
18 | wait = function(n, cb) {
19 | setTimeout(function() {
20 | cb(n);
21 | }, 100);
22 | },
23 |
24 | // Asynchronous function returning a promise
25 | wait2 = function(n) {
26 | var defer = when.defer();
27 | setTimeout(function() {
28 | defer.resolve(n);
29 | }, 100);
30 | return defer.promise;
31 | },
32 |
33 | // Create a timed verion of the Synchronous funciton
34 | fibT = timely(fib),
35 | // Create a timed verion of the Asynchronous function
36 | waitT = timely.async(wait),
37 | // Create a timed verion of the Asynchronous function returning a promise
38 | waitT2 = timely.promise(wait2),
39 |
40 | // Result of Synchronous function
41 | resultSync;
42 |
43 | // Call Synchronous function
44 | resultSync = fibT(35);
45 | //Output results of Synchronous function. fibT.time contains the time used for the last call
46 | console.log('fib(35) = ' + resultSync + ', time: ' + fibT.time + 'ms');
47 |
48 | // Call Asynchronous function
49 | waitT(42, function(resultAsync) {
50 | //Output results of Asynchronous function. fibT.time contains the time used for the last call
51 | console.log('wait(42) = ' + resultAsync + ', time: ' + waitT.time + 'ms');
52 | });
53 |
54 | // Call Asynchronous function returning a promise
55 | waitT2(42).then(function(resultAsync) {
56 | //Output results of Asynchronous function. waitT2.time contains the time used for the last call
57 | console.log('wait2(42) = ' + resultAsync + ', time: ' + waitT2.time + 'ms');
58 | });
59 |
--------------------------------------------------------------------------------
/lib/timely.js:
--------------------------------------------------------------------------------
1 | // Timely v0.0.2
2 | // Lightweight and easy to use timing decorators for javascript functions
3 | // (c) 2013 Aron Kornhall
4 | // Timely is freely distributable under the MIT license.
5 |
6 | (function() {
7 | var root = this;
8 |
9 | // Synchronous
10 | var timely = function(func, context) {
11 | var callCount = 0;
12 | var totalCalltime = 0;
13 | var timedFunc = function() {
14 | context = context || this;
15 | var begin = Date.now();
16 | var result = func.apply(context, arguments);
17 | var end = Date.now();
18 | var callTime = end - begin;
19 |
20 | totalCalltime += callTime;
21 | callCount++;
22 |
23 | timedFunc.time = callTime;
24 | timedFunc.totalCalltime = totalCalltime;
25 | timedFunc.meanTime = totalCalltime / callCount;
26 | timedFunc.callCount = callCount;
27 | return result;
28 | };
29 | return timedFunc;
30 | };
31 |
32 | // Asynchronous
33 | timely.async = function(func, context) {
34 | var callCount = 0;
35 | var totalCalltime = 0;
36 | var slice = Array.prototype.slice,
37 | timedFunc = function() {
38 | var args = slice.call(arguments); // Convert arguments to a real array
39 |
40 | context = context || this;
41 | var cb = args.pop(); // Pop old callback
42 | var timedCb = function() { // Create a new callback
43 | var end = Date.now();
44 | var callTime = end - begin;
45 | totalCalltime += callTime;
46 | callCount++;
47 |
48 | timedFunc.time = callTime;
49 | timedFunc.totalCalltime = totalCalltime;
50 | timedFunc.meanTime = totalCalltime / callCount;
51 | timedFunc.callCount = callCount;
52 | cb.apply(context, arguments);
53 | };
54 | args.push(timedCb); // Push new callback to args
55 | var begin = Date.now();
56 | return func.apply(context, args);
57 | };
58 | return timedFunc;
59 | };
60 |
61 | // Promise
62 | timely.promise = function(func, context) {
63 | var callCount = 0;
64 | var totalCalltime = 0;
65 | var slice = Array.prototype.slice,
66 | timedFunc = function() {
67 | context = context || this;
68 | var begin = Date.now();
69 | return func.apply(context, arguments).then(function(ret){
70 | var end = Date.now();
71 | var callTime = end - begin;
72 | totalCalltime += callTime;
73 | callCount++;
74 |
75 | timedFunc.time = callTime;
76 | timedFunc.totalCalltime = totalCalltime;
77 | timedFunc.meanTime = totalCalltime / callCount;
78 | timedFunc.callCount = callCount;
79 | return ret;
80 | });
81 | };
82 | return timedFunc;
83 | };
84 |
85 | // Expose timely as node module, amd module or global
86 | if(typeof module !== 'undefined' && module.exports){
87 | module.exports = timely;
88 | }else{
89 | if(typeof define === 'function' && define.amd) {
90 | define([], function() {
91 | return timely;
92 | });
93 | }else{
94 | root['timely'] = timely;
95 | }
96 | }
97 | }).call(this);
98 |
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) 2012 Aron Kornhall
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "author": "Aron Kornhall ",
3 | "name": "timely",
4 | "description": "Lightweight and easy to use timing decorators",
5 | "keywords": [
6 | "timing",
7 | "measure time"
8 | ],
9 | "version": "0.2.1",
10 | "homepage": "https://github.com/arokor/timely",
11 | "repository": {
12 | "type": "git",
13 | "url": "git://github.com/arokor/timely.git"
14 | },
15 | "bugs": "https://github.com/arokor/timely/issues",
16 | "main": "./lib/timely.js",
17 | "scripts": {
18 | "test": "nodeunit test"
19 | },
20 | "engines": {
21 | "node": ">=0.6.6"
22 | },
23 | "dependencies": {},
24 | "devDependencies": {
25 | "nodeunit": "0.7.x",
26 | "when": "~2.3.0"
27 | },
28 | "licences": [
29 | {
30 | "type": "MIT License",
31 | "url": "https://raw.github.com/arokor/timely/master/license.txt"
32 | }
33 | ]
34 | }
35 |
--------------------------------------------------------------------------------
/test/test-timely.js:
--------------------------------------------------------------------------------
1 | // Nodeunit tests for Timely
2 |
3 | var timely = require('../lib/timely');
4 | var when = require('when');
5 |
6 |
7 | // Test synchronous calls
8 | exports.sync = {
9 | testTimeFunction: function (test) {
10 | // Fibonacci
11 | function fib(n) {
12 | if (n <= 2) return n;
13 | return fib(n - 1) + fib(n - 2);
14 | }
15 |
16 | // Timed Fibonacci
17 | var fibT = timely(fib),
18 | n = 35,
19 |
20 | exp = fib(n),
21 | act = fibT(n);
22 |
23 | test.strictEqual(act, exp);
24 | test.ok(typeof fibT.time === 'number', 'Expected numeric time');
25 | test.done();
26 | },
27 | testTimeMethod: function(test) {
28 | // Object
29 | var o = {
30 | x : 5,
31 | f : function(n) {
32 | var i, result = 0;
33 | for (i=0; i t20 && tm2 < t30, 'Expected mean time to be between min and max');
78 | test.done();
79 | },
80 | };
81 |
82 |
83 | // Test asynchronous calls
84 | exports.async = {
85 | testTimeFunction: function (test) {
86 | // Async function
87 | function f(n, cb) {
88 | setTimeout(function() { cb(n); }, 100);
89 | }
90 |
91 | // Timed async function
92 | var fT = timely.async(f),
93 | n = 42;
94 |
95 | f(n, function(exp) {
96 | fT(n, function(act) {
97 | test.strictEqual(act, exp);
98 | test.ok(typeof fT.time === 'number', 'Expected numeric time');
99 | test.done();
100 | });
101 | });
102 | },
103 | testTimeMethod: function(test) {
104 | // Object
105 | var o = {
106 | x : 5,
107 | f : function(n, cb) {
108 | setTimeout((function(that) {
109 | return function() {
110 | var i, result = 0;
111 | for (i=0; i t10 && tm2 < t20, 'Expected mean time to be between min and max');
162 | test.done();
163 | });
164 | });
165 | },
166 | };
167 |
168 | // Test promised calls
169 | exports.promise = {
170 | testTimeFunction: function (test) {
171 | // Async function
172 | function f(n) {
173 | var defer = when.defer();
174 | setTimeout(function() { defer.resolve(n); }, 100);
175 | return defer.promise;
176 | }
177 |
178 | // Timed async function
179 | var fT = timely.promise(f),
180 | n = 42;
181 |
182 | f(n).then(function(exp) {
183 | fT(n).then(function(act) {
184 | test.strictEqual(act, exp);
185 | test.ok(typeof fT.time === 'number', 'Expected numeric time');
186 | test.done();
187 | });
188 | });
189 | },
190 | testTimeMethod: function(test) {
191 | // Object
192 | var o = {
193 | x : 5,
194 | f : function(n) {
195 | var _this = this;
196 | var defer = when.defer();
197 | setTimeout(function() {
198 | var i, result = 0;
199 | for (i=0; i t10 && tm2 < t20, 'Expected mean time to be between min and max');
253 | test.done();
254 | });
255 | });
256 | },
257 | };
258 |
--------------------------------------------------------------------------------