|
| 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 isArray = require( '@stdlib/assert/is-array' ); |
| 25 | +var isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 26 | +var pkg = require( './../package.json' ).name; |
| 27 | +var find = require( './../lib' ); // eslint-disable-line stdlib/no-redeclare |
| 28 | + |
| 29 | + |
| 30 | +// MAIN // |
| 31 | + |
| 32 | +bench( pkg+':returns=indices', function benchmark( b ) { |
| 33 | + var out; |
| 34 | + var arr; |
| 35 | + var i; |
| 36 | + |
| 37 | + function predicate( v ) { |
| 38 | + return isnan( v ); |
| 39 | + } |
| 40 | + b.tic(); |
| 41 | + for ( i = 0; i < b.iterations; i++ ) { |
| 42 | + arr = [ i, i+1, i+2, NaN, i+4, NaN ]; |
| 43 | + out = find( arr, predicate ); |
| 44 | + if ( typeof out !== 'object' ) { |
| 45 | + b.fail( 'should return an array' ); |
| 46 | + } |
| 47 | + } |
| 48 | + b.toc(); |
| 49 | + if ( !isArray( out ) ) { |
| 50 | + b.fail( 'should return an array' ); |
| 51 | + } |
| 52 | + b.pass( 'benchmark finished' ); |
| 53 | + b.end(); |
| 54 | +}); |
| 55 | + |
| 56 | +bench( pkg+':returns=values', function benchmark( b ) { |
| 57 | + var opts; |
| 58 | + var out; |
| 59 | + var arr; |
| 60 | + var i; |
| 61 | + |
| 62 | + function predicate( v ) { |
| 63 | + return isnan( v ); |
| 64 | + } |
| 65 | + opts = { |
| 66 | + 'returns': 'values' |
| 67 | + }; |
| 68 | + b.tic(); |
| 69 | + for ( i = 0; i < b.iterations; i++ ) { |
| 70 | + arr = [ i, i+1, i+2, NaN, i+4, NaN ]; |
| 71 | + out = find( arr, opts, predicate ); |
| 72 | + if ( typeof out !== 'object' ) { |
| 73 | + b.fail( 'should return an array' ); |
| 74 | + } |
| 75 | + } |
| 76 | + b.toc(); |
| 77 | + if ( !isArray( out ) ) { |
| 78 | + b.fail( 'should return an array' ); |
| 79 | + } |
| 80 | + b.pass( 'benchmark finished' ); |
| 81 | + b.end(); |
| 82 | +}); |
| 83 | + |
| 84 | +bench( pkg+':returns=*', function benchmark( b ) { |
| 85 | + var opts; |
| 86 | + var out; |
| 87 | + var arr; |
| 88 | + var i; |
| 89 | + |
| 90 | + function predicate( v ) { |
| 91 | + return isnan( v ); |
| 92 | + } |
| 93 | + opts = { |
| 94 | + 'returns': '*' |
| 95 | + }; |
| 96 | + b.tic(); |
| 97 | + for ( i = 0; i < b.iterations; i++ ) { |
| 98 | + arr = [ i, i+1, i+2, NaN, i+4, NaN ]; |
| 99 | + out = find( arr, opts, predicate ); |
| 100 | + if ( typeof out !== 'object' ) { |
| 101 | + b.fail( 'should return an array' ); |
| 102 | + } |
| 103 | + } |
| 104 | + b.toc(); |
| 105 | + if ( !isArray( out ) ) { |
| 106 | + b.fail( 'should return an array' ); |
| 107 | + } |
| 108 | + b.pass( 'benchmark finished' ); |
| 109 | + b.end(); |
| 110 | +}); |
0 commit comments