|
| 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 zeroTo = require( '@stdlib/array/zero-to' ); |
| 26 | +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; |
| 27 | +var factory = require( './../lib' ); |
| 28 | +var pkg = require( './../package.json' ).name; |
| 29 | + |
| 30 | + |
| 31 | +// VARIABLES // |
| 32 | + |
| 33 | +var Float64ArrayFE = factory( 'float64' ); |
| 34 | + |
| 35 | + |
| 36 | +// FUNCTIONS // |
| 37 | + |
| 38 | +/** |
| 39 | +* Creates a benchmark function. |
| 40 | +* |
| 41 | +* @private |
| 42 | +* @param {PositiveInteger} len - array length |
| 43 | +* @returns {Function} benchmark function |
| 44 | +*/ |
| 45 | +function createBenchmark( len ) { |
| 46 | + var arr = new Float64ArrayFE( 'little-endian', zeroTo( len ) ); |
| 47 | + return benchmark; |
| 48 | + |
| 49 | + /** |
| 50 | + * Benchmark function. |
| 51 | + * |
| 52 | + * @private |
| 53 | + * @param {Benchmark} b - benchmark instance |
| 54 | + */ |
| 55 | + function benchmark( b ) { |
| 56 | + var idx; |
| 57 | + var v; |
| 58 | + var i; |
| 59 | + |
| 60 | + v = len - 1; |
| 61 | + |
| 62 | + b.tic(); |
| 63 | + for ( i = 0; i < b.iterations; i++ ) { |
| 64 | + idx = arr.indexOf( v ); |
| 65 | + if ( typeof idx !== 'number' ) { |
| 66 | + b.fail( 'should return an integer' ); |
| 67 | + } |
| 68 | + } |
| 69 | + b.toc(); |
| 70 | + if ( !isInteger( idx ) ) { |
| 71 | + b.fail( 'should return an integer' ); |
| 72 | + } |
| 73 | + b.pass( 'benchmark finished' ); |
| 74 | + b.end(); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +// MAIN // |
| 80 | + |
| 81 | +/** |
| 82 | +* Main execution sequence. |
| 83 | +* |
| 84 | +* @private |
| 85 | +*/ |
| 86 | +function main() { |
| 87 | + var len; |
| 88 | + var min; |
| 89 | + var max; |
| 90 | + var f; |
| 91 | + var i; |
| 92 | + |
| 93 | + min = 1; // 10^min |
| 94 | + max = 6; // 10^max |
| 95 | + |
| 96 | + for ( i = min; i <= max; i++ ) { |
| 97 | + len = pow( 10, i ); |
| 98 | + f = createBenchmark( len ); |
| 99 | + bench( pkg+':indexOf:len='+len, f ); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +main(); |
0 commit comments