Skip to content

Commit a3581c6

Browse files
committed
Add benchmarks
1 parent b18b9e1 commit a3581c6

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isArray = require( '@stdlib/assert/is-array' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var pkg = require( './../package.json' ).name;
27+
var HAS_BUILTIN = require( './../lib/has_builtin.js' );
28+
var polyfill = require( './../lib/polyfill.js' );
29+
var wrapper = require( './../lib/builtin.js' );
30+
var argsWrapper = require( './../lib/builtin_wrapper.js' );
31+
var keys = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var opts = {
37+
'skip': !HAS_BUILTIN
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( pkg, function benchmark( b ) {
44+
var out;
45+
var obj;
46+
var i;
47+
48+
function Foo() {
49+
this.a = 'beep';
50+
this.b = 'boop';
51+
this.c = [ 1, 2, 3 ];
52+
this.d = {};
53+
this.e = null;
54+
this.f = randu();
55+
return this;
56+
}
57+
58+
Foo.prototype.g = [ 'foo' ];
59+
60+
obj = new Foo();
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
obj.f = randu();
65+
out = keys( obj );
66+
if ( !isArray( out ) ) {
67+
b.fail( 'should return an array' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isArray( out ) ) {
72+
b.fail( 'should return an array' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
});
77+
78+
bench( pkg+'::polyfill', function benchmark( b ) {
79+
var out;
80+
var obj;
81+
var i;
82+
83+
function Foo() {
84+
this.a = 'beep';
85+
this.b = 'boop';
86+
this.c = [ 1, 2, 3 ];
87+
this.d = {};
88+
this.e = null;
89+
this.f = randu();
90+
return this;
91+
}
92+
93+
Foo.prototype.g = [ 'foo' ];
94+
95+
obj = new Foo();
96+
97+
b.tic();
98+
for ( i = 0; i < b.iterations; i++ ) {
99+
obj.f = randu();
100+
out = polyfill( obj );
101+
if ( !isArray( out ) ) {
102+
b.fail( 'should return an array' );
103+
}
104+
}
105+
b.toc();
106+
if ( !isArray( out ) ) {
107+
b.fail( 'should return an array' );
108+
}
109+
b.pass( 'benchmark finished' );
110+
b.end();
111+
});
112+
113+
bench( pkg+'::builtin', opts, function benchmark( b ) {
114+
var out;
115+
var obj;
116+
var i;
117+
118+
function Foo() {
119+
this.a = 'beep';
120+
this.b = 'boop';
121+
this.c = [ 1, 2, 3 ];
122+
this.d = {};
123+
this.e = null;
124+
this.f = randu();
125+
return this;
126+
}
127+
128+
Foo.prototype.g = [ 'foo' ];
129+
130+
obj = new Foo();
131+
132+
b.tic();
133+
for ( i = 0; i < b.iterations; i++ ) {
134+
obj.f = randu();
135+
out = Object.keys( obj );
136+
if ( !isArray( out ) ) {
137+
b.fail( 'should return an array' );
138+
}
139+
}
140+
b.toc();
141+
if ( !isArray( out ) ) {
142+
b.fail( 'should return an array' );
143+
}
144+
b.pass( 'benchmark finished' );
145+
b.end();
146+
});
147+
148+
bench( pkg+'::builtin_wrapper', opts, function benchmark( b ) {
149+
var out;
150+
var obj;
151+
var i;
152+
153+
function Foo() {
154+
this.a = 'beep';
155+
this.b = 'boop';
156+
this.c = [ 1, 2, 3 ];
157+
this.d = {};
158+
this.e = null;
159+
this.f = randu();
160+
return this;
161+
}
162+
163+
Foo.prototype.g = [ 'foo' ];
164+
165+
obj = new Foo();
166+
167+
b.tic();
168+
for ( i = 0; i < b.iterations; i++ ) {
169+
obj.f = randu();
170+
out = wrapper( obj );
171+
if ( !isArray( out ) ) {
172+
b.fail( 'should return an array' );
173+
}
174+
}
175+
b.toc();
176+
if ( !isArray( out ) ) {
177+
b.fail( 'should return an array' );
178+
}
179+
b.pass( 'benchmark finished' );
180+
b.end();
181+
});
182+
183+
bench( pkg+'::builtin_arguments_wrapper', opts, function benchmark( b ) {
184+
var out;
185+
var obj;
186+
var i;
187+
188+
function Foo() {
189+
this.a = 'beep';
190+
this.b = 'boop';
191+
this.c = [ 1, 2, 3 ];
192+
this.d = {};
193+
this.e = null;
194+
this.f = randu();
195+
return this;
196+
}
197+
198+
Foo.prototype.g = [ 'foo' ];
199+
200+
obj = new Foo();
201+
202+
b.tic();
203+
for ( i = 0; i < b.iterations; i++ ) {
204+
obj.f = randu();
205+
out = argsWrapper( obj );
206+
if ( !isArray( out ) ) {
207+
b.fail( 'should return an array' );
208+
}
209+
}
210+
b.toc();
211+
if ( !isArray( out ) ) {
212+
b.fail( 'should return an array' );
213+
}
214+
b.pass( 'benchmark finished' );
215+
b.end();
216+
});

0 commit comments

Comments
 (0)