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

Commit d9cdb10

Browse files
Zizzamiajeffbcross
authored andcommitted
test(orderBy): implement benchmark for ngRepeat with orderBy
1 parent 44cf2e1 commit d9cdb10

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

benchmarks/orderby-bp/app.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var app = angular.module('orderByBenchmark', []);
2+
3+
app.controller('DataController', function($rootScope, $scope) {
4+
this.ngRepeatCount = 5000;
5+
this.rows = [];
6+
var self = this;
7+
8+
$scope.benchmarkType = 'basic';
9+
10+
$scope.rawProperty = function(key) {
11+
return function(item) {
12+
return item[key];
13+
};
14+
};
15+
16+
// Returns a random integer between min (included) and max (excluded)
17+
function getRandomInt(min, max) {
18+
return Math.floor(Math.random() * (max - min)) + min;
19+
}
20+
21+
benchmarkSteps.push({
22+
name: 'setup',
23+
description: 'Set rows to empty array and apply, then push new rows to be applied in next step',
24+
fn: function() {
25+
var oldRows = self.rows;
26+
$rootScope.$apply(function() {
27+
self.rows = [];
28+
});
29+
self.rows = oldRows;
30+
if (self.rows.length !== self.ngRepeatCount) {
31+
self.rows = [];
32+
for (var i = 0; i < self.ngRepeatCount; i++) {
33+
self.rows.push({
34+
'name': getRandomInt(i, (i + 40)),
35+
'index': i
36+
});
37+
}
38+
}
39+
}
40+
})
41+
42+
benchmarkSteps.push({
43+
name: '$apply',
44+
fn: function() {
45+
$rootScope.$apply();
46+
}
47+
});
48+
});

benchmarks/orderby-bp/bp.conf.js

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

benchmarks/orderby-bp/main.html

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<div class="container-fluid" ng-app="filtersBenchmark">
2+
<div class="row" ng-controller="DataController as ctrl">
3+
<div class="col-lg-8">
4+
<p>Filters</p>
5+
6+
<p>
7+
<label>Number of ngRepeats:</label>
8+
<input type="number" ng-model="ctrl.ngRepeatCount">
9+
</p>
10+
11+
<p>
12+
<div class="radio">
13+
<label>
14+
<input type="radio" ng-model="benchmarkType" value="baseline">baseline
15+
</label>
16+
</div>
17+
<pre><code>ng-repeat="row in ctrl.rows"</code></pre>
18+
<br />
19+
<div class="radio">
20+
<label>
21+
<input type="radio" ng-model="benchmarkType" value="orderBy">orderBy
22+
</label>
23+
</div>
24+
<pre><code>ng-repeat="row in ctrl.rows | orderBy:'name'"</code></pre>
25+
<br />
26+
<div class="radio">
27+
<label>
28+
<input type="radio" ng-model="benchmarkType" value="orderByArray">orderBy array expression
29+
</label>
30+
</div>
31+
<pre><code>ng-repeat="row in ctrl.rows | orderBy:['name', 'index']"</code></pre>
32+
<br />
33+
<div class="radio">
34+
<label>
35+
<input type="radio" ng-model="benchmarkType"
36+
value="orderByFunction">orderBy function expression
37+
</label>
38+
</div>
39+
<pre><code>ng-repeat="row in ctrl.rows | orderBy:rawProperty('name')"</code></pre>
40+
<br />
41+
<div class="radio">
42+
<label>
43+
<input type="radio" ng-model="benchmarkType"
44+
value="orderByArrayFunction">orderBy array function expression
45+
</label>
46+
</div>
47+
<pre><code>ng-repeat="row in ctrl.rows | orderBy:[rawProperty('name'), rawProperty('index')]"</code></pre>
48+
</p>
49+
50+
51+
Debug output:
52+
<ng-switch on="benchmarkType">
53+
<div ng-switch-when="baseline">
54+
<span ng-repeat="row in ctrl.rows">
55+
<span ng-bind="row.name"></span>,
56+
</span>
57+
</div>
58+
<div ng-switch-when="orderBy">
59+
<span ng-repeat="row in ctrl.rows | orderBy:'name'">
60+
<span ng-bind="row.name"></span>,
61+
</span>
62+
</div>
63+
<div ng-switch-when="orderByArray">
64+
<span ng-repeat="row in ctrl.rows | orderBy:['name', 'index']">
65+
<span ng-bind="row.name"></span>,
66+
</span>
67+
</div>
68+
<div ng-switch-when="orderByFunction">
69+
<span ng-repeat="row in ctrl.rows | orderBy:rawProperty('name')">
70+
<span ng-bind="row.name"></span>,
71+
</span>
72+
</div>
73+
<div ng-switch-when="orderByArrayFunction">
74+
<span ng-repeat="row in ctrl.rows | orderBy:[rawProperty('name'), rawProperty('index')]">
75+
<span ng-bind="row.name"></span>,
76+
</span>
77+
</div>
78+
</ng-switch>
79+
80+
</div>
81+
</div>
82+
</div>

0 commit comments

Comments
 (0)