Skip to content

Commit 1ccdba3

Browse files
committed
bench: refactor benchmarks
1 parent 0679aa1 commit 1ccdba3

File tree

8 files changed

+603
-225
lines changed

8 files changed

+603
-225
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
26+
var tryRequire = require( '@stdlib/utils/try-require' );
27+
var countBy = require( '@stdlib/utils/count-by' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var _ = tryRequire( resolve( __dirname, '..', 'node_modules', 'lodash' ) );
34+
var opts = {
35+
'skip': ( _ instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::stdlib:utils/count-by', opts, function benchmark( b ) {
42+
var vals;
43+
var arr;
44+
var o;
45+
var i;
46+
var j;
47+
48+
vals = [ 'beep', 'boop', 'foo', 'bar', 'woot' ];
49+
50+
arr = [];
51+
for ( i = 0; i < 100; i++ ) {
52+
j = discreteUniform( 0, vals.length-1 );
53+
arr.push( vals[ j ] );
54+
}
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
arr[ 0 ] = vals[ i%vals.length ];
58+
o = countBy( arr, indicator );
59+
if ( typeof o !== 'object' ) {
60+
b.fail( 'should return an object' );
61+
}
62+
}
63+
b.toc();
64+
if ( typeof o !== 'object' ) {
65+
b.fail( 'should return an object' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
70+
function indicator( v ) {
71+
return v[ 0 ];
72+
}
73+
});
74+
75+
bench( pkg+'::lodash:countBy', opts, function benchmark( b ) {
76+
var vals;
77+
var arr;
78+
var o;
79+
var i;
80+
var j;
81+
82+
vals = [ 'beep', 'boop', 'foo', 'bar', 'woot' ];
83+
84+
arr = [];
85+
for ( i = 0; i < 100; i++ ) {
86+
j = discreteUniform( 0, vals.length-1 );
87+
arr.push( vals[ j ] );
88+
}
89+
b.tic();
90+
for ( i = 0; i < b.iterations; i++ ) {
91+
arr[ 0 ] = vals[ i%vals.length ];
92+
o = _.countBy( arr, indicator );
93+
if ( typeof o !== 'object' ) {
94+
b.fail( 'should return an object' );
95+
}
96+
}
97+
b.toc();
98+
if ( typeof o !== 'object' ) {
99+
b.fail( 'should return an object' );
100+
}
101+
b.pass( 'benchmark finished' );
102+
b.end();
103+
104+
function indicator( v ) {
105+
return v[ 0 ];
106+
}
107+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var every = require( '@stdlib/utils/every' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var _ = tryRequire( resolve( __dirname, '..', 'node_modules', 'lodash' ) );
35+
var opts = {
36+
'skip': ( _ instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::stdlib:utils/every', opts, function benchmark( b ) {
43+
var bool;
44+
var arr;
45+
var i;
46+
47+
arr = [ 1, 2, 3, 4, 5 ];
48+
49+
b.tic();
50+
for ( i = 0; i < b.iterations; i++ ) {
51+
arr[ 0 ] += i;
52+
bool = every( arr, predicate );
53+
if ( typeof bool !== 'boolean' ) {
54+
b.fail( 'should return a boolean' );
55+
}
56+
}
57+
b.toc();
58+
if ( !isBoolean( bool ) ) {
59+
b.fail( 'should return a boolean' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
64+
function predicate( v ) {
65+
return !isnan( v );
66+
}
67+
});
68+
69+
bench( pkg+'::lodash:every', opts, function benchmark( b ) {
70+
var bool;
71+
var arr;
72+
var i;
73+
74+
arr = [ 1, 2, 3, 4, 5 ];
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
arr[ 0 ] += i;
79+
bool = _.every( arr, predicate );
80+
if ( typeof bool !== 'boolean' ) {
81+
b.fail( 'should return a boolean' );
82+
}
83+
}
84+
b.toc();
85+
if ( !isBoolean( bool ) ) {
86+
b.fail( 'should return a boolean' );
87+
}
88+
b.pass( 'benchmark finished' );
89+
b.end();
90+
91+
function predicate( v ) {
92+
return !isnan( v );
93+
}
94+
});
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var tryRequire = require( '@stdlib/utils/try-require' );
27+
var forEach = require( '@stdlib/utils/for-each' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var _ = tryRequire( resolve( __dirname, '..', 'node_modules', 'lodash' ) );
34+
var opts = {
35+
'skip': ( _ instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::stdlib:utils/for-each', opts, function benchmark( b ) {
42+
var arr;
43+
var i;
44+
45+
arr = [ 1, 2, 3, 4, 5 ];
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
arr[ 0 ] += i;
50+
forEach( arr, onItem );
51+
if ( arr.length !== 5 ) {
52+
b.fail( 'should not change the array length' );
53+
}
54+
}
55+
b.toc();
56+
if ( arr.length !== 5 ) {
57+
b.fail( 'should not change the array length' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
62+
function onItem( v ) {
63+
if ( isnan( v ) ) {
64+
b.fail( 'should not be NaN' );
65+
}
66+
}
67+
});
68+
69+
bench( pkg+'::lodash:forEach', opts, function benchmark( b ) {
70+
var arr;
71+
var i;
72+
73+
arr = [ 1, 2, 3, 4, 5 ];
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
arr[ 0 ] += i;
78+
_.forEach( arr, onItem );
79+
if ( arr.length !== 5 ) {
80+
b.fail( 'should not change the array length' );
81+
}
82+
}
83+
b.toc();
84+
if ( arr.length !== 5 ) {
85+
b.fail( 'should not change the array length' );
86+
}
87+
b.pass( 'benchmark finished' );
88+
b.end();
89+
90+
function onItem( v ) {
91+
if ( isnan( v ) ) {
92+
b.fail( 'should not be NaN' );
93+
}
94+
}
95+
});
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var tryRequire = require( '@stdlib/utils/try-require' );
27+
var forEachRight = require( '@stdlib/utils/for-each-right' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var _ = tryRequire( resolve( __dirname, '..', 'node_modules', 'lodash' ) );
34+
var opts = {
35+
'skip': ( _ instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::stdlib:utils/for-each-right', opts, function benchmark( b ) {
42+
var arr;
43+
var i;
44+
45+
arr = [ 1, 2, 3, 4, 5 ];
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
arr[ 0 ] += i;
50+
forEachRight( arr, onItem );
51+
if ( arr.length !== 5 ) {
52+
b.fail( 'should not change the array length' );
53+
}
54+
}
55+
b.toc();
56+
if ( arr.length !== 5 ) {
57+
b.fail( 'should not change the array length' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
62+
function onItem( v ) {
63+
if ( isnan( v ) ) {
64+
b.fail( 'should not be NaN' );
65+
}
66+
}
67+
});
68+
69+
bench( pkg+'::lodash:forEachRight', opts, function benchmark( b ) {
70+
var arr;
71+
var i;
72+
73+
arr = [ 1, 2, 3, 4, 5 ];
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
arr[ 0 ] += i;
78+
_.forEachRight( arr, onItem );
79+
if ( arr.length !== 5 ) {
80+
b.fail( 'should not change the array length' );
81+
}
82+
}
83+
b.toc();
84+
if ( arr.length !== 5 ) {
85+
b.fail( 'should not change the array length' );
86+
}
87+
b.pass( 'benchmark finished' );
88+
b.end();
89+
90+
function onItem( v ) {
91+
if ( isnan( v ) ) {
92+
b.fail( 'should not be NaN' );
93+
}
94+
}
95+
});

0 commit comments

Comments
 (0)