DCL

An elegant OOP with mixins + AOP for JavaScript.

time()

Version 1.x

time() creates a named timer using a standard console interface: console.time(name) and console.timeEnd(name).

It can be included with following commands:

Include time()
1
2
3
4
5
6
7
8
9
10
11
12
13
// node.js
var time = require("dcl/advices/time");
...

// AMD (code)
require(["dcl/advices/time"], function(time){
  ...
});

// AMD (definition)
define(["dcl/advices/time"], function(time){
  ...
});

Description

The result value of time module is a function, which takes a string parameter name, and returns an advice object, which can be used directly with dcl.advise() or advise().

The advice prints on console when a method was invoked, and when it finished. Recursive calls are allowed, but only first invocation is printed.

If name is not specified, a unique name is generated.

Examples

time() example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var Stack = dcl(null, {
  declaredClass: "Stack",
  constructor: function(){
    this.stack = [];
  },
  push: function(n){
    return this.stack.push(n);
  },
  pop: function(){
    return this.stack.pop();
  },
  sum: function(init){
    // expensive, yet frequently called method
    // it has a linear complexity on stack size
    var acc = init;
    for(var i = 0; i < this.stack.length; ++i){
      acc += this.stack[i];
    }
    return acc;
  }
});

var x = new Stack();

advise(x, "sum", time("sum"));
x.push(1);
x.push(2);
x.push(3);
var n = x.sum(0);

The example above will print something like that:

1
sum: 0.089ms