-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks.js
141 lines (115 loc) · 3.61 KB
/
benchmarks.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var keylime = require('./');
var _ = require('lodash');
var KeylimePost = keylime('KeylimePost')
.attr('draft', true)
.attr('title', '')
.attr('tags', [])
.attr('date', function createDate() {
return Date.now();
});
var HybridPost = keylime(function HybridPost(title, options) {
this.keylime.init(this, options);
this.title = title;
})
.attr('title', '')
.attr('draft', true)
.attr('tags', [])
.attr('date', function() {
return Date.now();
});
var plainAttrs = {
title: '',
tags: [],
draft: true,
date: function createDate() {
return Date.now();
}
};
function BarePost(params) {
this.title = params.title || '';
this.tags = params.tags || [];
this.draft = params.draft || true;
this.date = params.date || Date.now();
}
function PlainPost(params) {
var value;
for (var attrName in plainAttrs) {
if (plainAttrs.hasOwnProperty(attrName)) {
if (params && params[attrName] !== undefined) {
value = params[attrName];
} else {
value = plainAttrs[attrName];
}
if (typeof value === 'function') {
this[attrName] = value();
} else {
this[attrName] = value;
}
}
}
}
suite('creating instances with \'new\'', function() {
bench('bare post', function() {
new BarePost({title: 'New Post'});
});
bench('plain constructor', function() {
new PlainPost({title: 'New Post'});
});
bench('keylime', function() {
new KeylimePost({title: 'New Post'});
});
bench('hybrid', function() {
new HybridPost('New Post');
});
});
suite('init handlers', function() {
var Post1 = keylime('Post').attr('attr1', 'value1').on('init', function() {});
var Post3 = keylime('Post').attr('attr1', 'value1').on('init', function() {}).on('init', function() {}).on('init', function() {});
var Post5 = keylime('Post').attr('attr1', 'value1').on('init', function() {}).on('init', function() {}).on('init', function() {}).on('init', function() {}).on('init', function() {});
bench('1 handlers', function() {
new Post1();
});
bench('3 handlers', function() {
new Post3();
});
bench('5 handlers', function() {
new Post5();
});
});
suite('attribute handlers', function() {
var Post1 = keylime('Post').attr('attr1', 'value1', {handlers: function() { return 1;} });
var Post3 = keylime('Post').attr('attr1', 'value1', {handlers: [function() { return 1;}, function(value) { return ++value; }, function(value) { return ++value; }]});
var Post5 = keylime('Post').attr('attr1', 'value1', {handlers: [function() { return 1;}, function(value) { return ++value; }, function(value) { return ++value; }, function(value) { return ++value; }, function(value) { return ++value; }]});
bench('1 handlers', function() {
new Post1();
});
bench('3 handlers', function() {
new Post3();
});
bench('5 handlers', function() {
new Post5();
});
});
suite('using \'new\' vs .create()', function() {
var Post = keylime('Post').attr('title').attr('date', Date.now).attr('comments', []);
bench('new', function() {
new Post({title: 'hello world'});
});
bench('.create()', function() {
Post.create({title: 'hello world'});
});
});
suite('attribute counts', function() {
var Jedi0 = keylime('Jedi');
var Jedi3 = keylime('Jedi').attr('one', 1).attr('two', 2).attr('three', 3);
var Jedi5 = keylime('Jedi').attr('one', 1).attr('two', 2).attr('three', 3).attr('four', 4).attr('five', 5);
bench('0 attributes', function() {
new Jedi0();
});
bench('3 attributes', function() {
new Jedi3();
});
bench('5 attributes', function() {
new Jedi5();
});
});