Skip to content

Commit f9b0d1f

Browse files
committed
Add benchmarks for number of properties and arguments
1 parent ce10a6f commit f9b0d1f

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
26+
var isEmptyArray = require( '@stdlib/assert/is-empty-array' );
27+
var pkg = require( './../package.json' ).name;
28+
var commonKeys = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} N - number of arguments
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( N ) {
41+
var args;
42+
var o;
43+
var i;
44+
var j;
45+
46+
args = [];
47+
for ( i = 0; i < N; i++ ) {
48+
o = {};
49+
for ( j = 0; j < 10; j++ ) {
50+
o[ j ] = i * j;
51+
}
52+
args.push( o );
53+
}
54+
return benchmark;
55+
56+
/**
57+
* Benchmark function.
58+
*
59+
* @private
60+
* @param {Benchmark} b - benchmark instance
61+
*/
62+
function benchmark( b ) {
63+
var keys;
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
keys = commonKeys.apply( null, args );
69+
if ( typeof keys !== 'object' ) {
70+
b.fail( 'should return an object' );
71+
}
72+
}
73+
b.toc();
74+
if ( !isStringArray( keys ) && !isEmptyArray( keys ) ) {
75+
b.fail( 'should return an array of strings' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 2^min
98+
max = 6; // 2^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 2, i );
102+
f = createBenchmark( len );
103+
bench( pkg+':num_args='+len+',num_properties=10', f );
104+
}
105+
}
106+
107+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
26+
var isEmptyArray = require( '@stdlib/assert/is-empty-array' );
27+
var pkg = require( './../package.json' ).name;
28+
var commonKeys = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} N - number of properties
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( N ) {
41+
var x;
42+
var y;
43+
var i;
44+
45+
x = {};
46+
y = {};
47+
for ( i = 0; i < N; i++ ) {
48+
x[ i ] = i;
49+
y[ i ] = i + 1;
50+
}
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var keys;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
keys = commonKeys( x, y );
66+
if ( typeof keys !== 'object' ) {
67+
b.fail( 'should return an object' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isStringArray( keys ) && !isEmptyArray( keys ) ) {
72+
b.fail( 'should return an array of strings' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( pkg+':num_properties='+len, f );
101+
}
102+
}
103+
104+
main();

0 commit comments

Comments
 (0)