Skip to content

Commit 6f1c381

Browse files
committed
feat: add benchmarks for entries method of array/fixed-endian-factory
1 parent 357d02f commit 6f1c381

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 pkg = require( './../package.json' ).name;
25+
var factory = require( './../lib' );
26+
27+
28+
// MAIN //
29+
30+
bench( pkg+':entries:len=10', function benchmark( b ) {
31+
var iter;
32+
var arr;
33+
var i;
34+
35+
arr = new factory( 'int32' )( 'little-endian', 10 );
36+
for ( i = 0; i < 10; i++ ) {
37+
arr.set( i, i );
38+
}
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
iter = arr.entries();
43+
if ( typeof iter !== 'object' ) {
44+
b.fail( 'should return an object' );
45+
}
46+
}
47+
b.toc();
48+
if ( typeof iter !== 'object' || typeof iter.next !== 'function' ) {
49+
b.fail( 'should return an iterator protocol-compliant object' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 pkg = require( './../package.json' ).name;
26+
var factory = require( './../lib' );
27+
28+
29+
// VARIABLES //
30+
31+
var Float64ArrayFE = factory( 'float64' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Creates a benchmark function.
38+
*
39+
* @private
40+
* @param {PositiveInteger} len - array length
41+
* @returns {Function} benchmark function
42+
*/
43+
function createBenchmark( len ) {
44+
var arr = new Float64ArrayFE( 'little-endian', len );
45+
return benchmark;
46+
47+
/**
48+
* Benchmark function.
49+
*
50+
* @private
51+
* @param {Benchmark} b - benchmark instance
52+
*/
53+
function benchmark( b ) {
54+
var itr;
55+
var i;
56+
57+
itr = arr.entries();
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
itr.next();
61+
if ( arr.length !== len ) {
62+
b.fail( 'should not change an array length' );
63+
}
64+
}
65+
b.toc();
66+
if ( arr.length !== len ) {
67+
b.fail( 'should not change an array length' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
}
72+
}
73+
74+
75+
// MAIN //
76+
77+
/**
78+
* Main execution sequence.
79+
*
80+
* @private
81+
*/
82+
function main() {
83+
var len;
84+
var min;
85+
var max;
86+
var f;
87+
var i;
88+
89+
min = 1; // 10^min
90+
max = 6; // 10^max
91+
92+
for ( i = min; i <= max; i++ ) {
93+
len = pow( 10, i );
94+
f = createBenchmark( len );
95+
bench( pkg+':every:len='+len, f );
96+
}
97+
}
98+
99+
main();

0 commit comments

Comments
 (0)