Skip to content

Commit 6863b98

Browse files
committed
Add benchmark
1 parent 1d99ea2 commit 6863b98

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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+
/* eslint-disable no-restricted-syntax */
20+
21+
'use strict';
22+
23+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
24+
var bench = require( './../lib' );
25+
26+
var opts = {
27+
'iterations': 1e9,
28+
'repeats': 3
29+
};
30+
31+
bench( 'ternary', opts, function benchmark( b ) {
32+
var r;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
r = (i&1) ? i+2 : 0;
38+
if ( r !== r ) {
39+
b.fail( 'something went wrong!' );
40+
}
41+
}
42+
b.toc();
43+
if ( isnan( r ) ) {
44+
b.fail( 'something went wrong!' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( 'short-circuit', opts, function benchmark( b ) {
51+
var r;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
r = (i&1) && i+2;
57+
if ( r !== r ) {
58+
b.fail( 'something went wrong!' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( r ) ) {
63+
b.fail( 'something went wrong!' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});

0 commit comments

Comments
 (0)