Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3969f4a

Browse files
committed
chore(benchpress): add a ngClass benchmark
1 parent 138fbf0 commit 3969f4a

File tree

3 files changed

+223
-0
lines changed

3 files changed

+223
-0
lines changed

benchmarks/ng-class-bp/app.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
3+
var app = angular.module('ngClassBenchmark', []);
4+
5+
app.controller('DataController', function DataController($scope) {
6+
7+
this.numberOfTodos = 1000;
8+
this.implementation = 'tableOptimized';
9+
this.completedPeriodicity = 3;
10+
this.importantPeriodicity = 23;
11+
this.urgentPeriodicity = 97;
12+
this.todos = buildTodos(100);
13+
configureTodos(this.todos, 0, this);
14+
15+
benchmarkSteps.push({
16+
name: 'setup',
17+
fn: function() {
18+
this.todos = [];
19+
$scope.$apply();
20+
this.todos = buildTodos(this.numberOfTodos);
21+
}.bind(this)
22+
});
23+
24+
benchmarkSteps.push({
25+
name: 'create',
26+
fn: function() {
27+
configureTodos(this.todos, 0, this);
28+
$scope.$apply();
29+
}.bind(this)
30+
});
31+
32+
benchmarkSteps.push({
33+
name: '$apply',
34+
fn: function() {
35+
$scope.$apply();
36+
}
37+
});
38+
39+
benchmarkSteps.push({
40+
name: 'update',
41+
fn: function() {
42+
configureTodos(this.todos, 1, this);
43+
$scope.$apply();
44+
}.bind(this)
45+
});
46+
47+
benchmarkSteps.push({
48+
name: 'unclass',
49+
fn: function() {
50+
configureTodos(this.todos, -1, this);
51+
$scope.$apply();
52+
}.bind(this)
53+
});
54+
55+
benchmarkSteps.push({
56+
name: 'class',
57+
fn: function() {
58+
configureTodos(this.todos, 0, this);
59+
$scope.$apply();
60+
}.bind(this)
61+
});
62+
63+
benchmarkSteps.push({
64+
name: 'destroy',
65+
fn: function() {
66+
this.todos.length = 0;
67+
$scope.$apply();
68+
}.bind(this)
69+
});
70+
71+
function buildTodos(count) {
72+
var todos = [];
73+
var i;
74+
for (i = 0; i < count; i++) {
75+
todos.push({
76+
id: i + 1,
77+
completed: false,
78+
important: false,
79+
urgent: false
80+
});
81+
}
82+
return todos;
83+
}
84+
85+
function configureTodos(todos, offset, config) {
86+
var i, todo;
87+
for (i = 0; i < todos.length; i++) {
88+
todo = todos[i];
89+
todo.completed = offset === i % config.completedPeriodicity;
90+
todo.important = offset === i % config.importantPeriodicity;
91+
todo.urgent = offset === i % config.urgentPeriodicity;
92+
}
93+
}
94+
95+
});

benchmarks/ng-class-bp/bp.conf.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-env node */
2+
3+
'use strict';
4+
5+
module.exports = function(config) {
6+
config.set({
7+
scripts: [{
8+
id: 'angular',
9+
src: '/build/angular.js'
10+
},
11+
{
12+
src: 'app.js'
13+
}]
14+
});
15+
};

benchmarks/ng-class-bp/main.html

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<style>
2+
.gold {
3+
background: gold;
4+
}
5+
.silver {
6+
background: silver;
7+
}
8+
.table tbody tr > td.success {
9+
background-color: #dff0d8;
10+
}
11+
12+
.table tbody tr > td.error {
13+
background-color: #f2dede;
14+
}
15+
16+
.table tbody tr > td.warning {
17+
background-color: #fcf8e3;
18+
}
19+
20+
.table tbody tr > td.info {
21+
background-color: #d9edf7;
22+
}
23+
.completed {
24+
text-decoration: line-through;
25+
}
26+
.important {
27+
font-weight: bold;
28+
}
29+
.urgent {
30+
color: red;
31+
}
32+
</style>
33+
<div ng-app="ngClassBenchmark" ng-cloak>
34+
<div ng-controller="DataController as benchmark">
35+
<h3>Parameters</h3>
36+
<p>
37+
<label>Number of todos</label><br>
38+
<input type="number" ng-model="benchmark.numberOfTodos">
39+
</p>
40+
<p>
41+
Implementation:<br>
42+
<label>
43+
<input type="radio" ng-model="benchmark.implementation" value="tableOptimized">
44+
Table optimized (condition && string-class)
45+
</label><br>
46+
<label>
47+
<input type="radio" ng-model="benchmark.implementation" value="table">
48+
Table ({class: condition})
49+
</label><br>
50+
<label>
51+
<input type="radio" ng-model="benchmark.implementation" value="list">
52+
Todos list ({completed: ..., urgent: ..., ...})
53+
</label><br>
54+
</p>
55+
56+
<br>
57+
<h3>Example</h3>
58+
<div ng-switch="benchmark.implementation">
59+
<table ng-switch-when="tableOptimized" class="table">
60+
<thead>
61+
<tr>
62+
<th>todo #id</th>
63+
<th>completed?</th>
64+
<th>urgent?</th>
65+
<th>important?</th>
66+
</tr>
67+
</thead>
68+
<tbody>
69+
<tr ng-repeat="todo in benchmark.todos track by todo.id"
70+
ng-class="todo.completed && 'active'"
71+
ng-class-even="todo.completed && todo.important && 'gold'"
72+
ng-class-odd="todo.completed && todo.important && 'silver'"
73+
>
74+
<td>#{{todo.id}}</td>
75+
<td>{{todo.completed}}</td>
76+
<td ng-class="todo.urgent && 'danger'">{{todo.urgent}}</td>
77+
<td ng-class="todo.important && 'success'">{{todo.important}}</td>
78+
</tr>
79+
</tbody>
80+
</table>
81+
<table ng-switch-when="table" class="table">
82+
<thead>
83+
<tr>
84+
<th>todo #id</th>
85+
<th>completed?</th>
86+
<th>urgent?</th>
87+
<th>important?</th>
88+
</tr>
89+
</thead>
90+
<tbody>
91+
<tr ng-repeat="todo in benchmark.todos track by todo.id"
92+
ng-class="{active: todo.completed}"
93+
ng-class-even="{gold: todo.completed && todo.important}"
94+
ng-class-odd="{silver: todo.completed && todo.important}"
95+
>
96+
<td>#{{todo.id}}</td>
97+
<td>{{todo.completed}}</td>
98+
<td ng-class="{danger: todo.urgent}">{{todo.urgent}}</td>
99+
<td ng-class="{success: todo.important}">{{todo.important}}</td>
100+
</tr>
101+
</tbody>
102+
</table>
103+
<ul ng-switch-when="list">
104+
<li ng-repeat="todo in benchmark.todos track by todo.id"
105+
ng-class="{
106+
completed: todo.completed,
107+
urgent: todo.urgent,
108+
important: todo.important
109+
}">#{{todo.id}}</li>
110+
</ul>
111+
</div>
112+
</div>
113+
</div>

0 commit comments

Comments
 (0)