summaryrefslogtreecommitdiffstats
path: root/bench/svg.bench.js
blob: 653d438f9eb5127595714ee1c35835dccf302bd4 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
;( function() {

  SVG.bench = {
    // Initalize test store
    _tests:  []
  , _before: function() {}
  , _after:  function() {}
  , draw:    SVG('draw')
  , raw:     document.getElementById('native')

    // Add test
  , test: function(name, closure) {
      this._tests.push({
        name: name
      , test: closure
      })

      return this
    }

    // Set before runner
  , before: function(closure) {
      this._before = closure

      return this
    }

    // Set after runner
  , after: function(closure) {
      this._after = closure

      return this
    }

    // Run tests
  , run: function() {
      this.pad(true)
      
      for (var s, i = 0, il = this._tests.length; i < il; i++) {
        // run before
        this._before(this._tests[i])

        // run test
        s = ( new Date ).getTime()
        this._tests[i].test()
        this.write( this._tests[i].name, ( new Date ).getTime() - s )

        // run after
        this._after(this._tests[i])

        // clear everything
        this.clear()
      }
    }

    // Write result
  , write: function(name, ms) {
      var test = document.createElement('div')
      test.className = 'test'
      test.innerHTML = 'Compleded "' + name + '" in ' + ms + 'ms'

      this.pad().appendChild(test)

      return this
    }

    // Reference writable element
  , pad: function(regenerate) {
      var pad = document.getElementById('pad')

      if (regenerate || !pad) {
        pad = document.createElement('div')
        document.getElementsByTagName('body')[0].appendChild(pad)
      }

      return pad
    }

    // Clear canvasses
  , clear: function() {
      while(this.raw.hasChildNodes())
        this.raw.removeChild(this.raw.lastChild)
      this.draw.clear()
    }
  }

})();