|
| 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 bench = require( '@stdlib/bench' ); |
| 24 | +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; |
| 25 | +var isFunction = require( '@stdlib/assert/is-function' ); |
| 26 | +var pkg = require( './../package.json' ).name; |
| 27 | +var factory = require( './../lib' ).factory; |
| 28 | + |
| 29 | + |
| 30 | +// MAIN // |
| 31 | + |
| 32 | +bench( pkg+':factory', function benchmark( b ) { |
| 33 | + var modes; |
| 34 | + var out; |
| 35 | + var i; |
| 36 | + |
| 37 | + modes = [ |
| 38 | + 'throw', |
| 39 | + 'clamp', |
| 40 | + 'wrap', |
| 41 | + 'normalize' |
| 42 | + ]; |
| 43 | + |
| 44 | + b.tic(); |
| 45 | + for ( i = 0; i < b.iterations; i++ ) { |
| 46 | + out = factory( modes[ i%modes.length ] ); |
| 47 | + if ( typeof out !== 'function' ) { |
| 48 | + b.fail( 'should return a function' ); |
| 49 | + } |
| 50 | + } |
| 51 | + b.toc(); |
| 52 | + if ( !isFunction( out ) ) { |
| 53 | + b.fail( 'should return a function' ); |
| 54 | + } |
| 55 | + b.pass( 'benchmark finished' ); |
| 56 | + b.end(); |
| 57 | +}); |
| 58 | + |
| 59 | +bench( pkg+':factory:mode=clamp', function benchmark( b ) { |
| 60 | + var ind; |
| 61 | + var out; |
| 62 | + var idx; |
| 63 | + var i; |
| 64 | + |
| 65 | + ind = factory( 'clamp' ); |
| 66 | + |
| 67 | + b.tic(); |
| 68 | + for ( i = 0; i < b.iterations; i++ ) { |
| 69 | + idx = (i%100) - 50; |
| 70 | + out = ind( idx, 10 ); |
| 71 | + if ( out !== out ) { |
| 72 | + b.fail( 'should not return NaN' ); |
| 73 | + } |
| 74 | + } |
| 75 | + b.toc(); |
| 76 | + if ( !isNonNegativeInteger( out ) ) { |
| 77 | + b.fail( 'should return a nonnegative integer' ); |
| 78 | + } |
| 79 | + b.pass( 'benchmark finished' ); |
| 80 | + b.end(); |
| 81 | +}); |
| 82 | + |
| 83 | +bench( pkg+':factory:mode=wrap', function benchmark( b ) { |
| 84 | + var ind; |
| 85 | + var out; |
| 86 | + var idx; |
| 87 | + var i; |
| 88 | + |
| 89 | + ind = factory( 'wrap' ); |
| 90 | + |
| 91 | + b.tic(); |
| 92 | + for ( i = 0; i < b.iterations; i++ ) { |
| 93 | + idx = (i%100) - 50; |
| 94 | + out = ind( idx, 10 ); |
| 95 | + if ( out !== out ) { |
| 96 | + b.fail( 'should not return NaN' ); |
| 97 | + } |
| 98 | + } |
| 99 | + b.toc(); |
| 100 | + if ( !isNonNegativeInteger( out ) ) { |
| 101 | + b.fail( 'should return a nonnegative integer' ); |
| 102 | + } |
| 103 | + b.pass( 'benchmark finished' ); |
| 104 | + b.end(); |
| 105 | +}); |
| 106 | + |
| 107 | +bench( pkg+':factory:mode=throw', function benchmark( b ) { |
| 108 | + var ind; |
| 109 | + var out; |
| 110 | + var idx; |
| 111 | + var i; |
| 112 | + |
| 113 | + ind = factory( 'throw' ); |
| 114 | + |
| 115 | + b.tic(); |
| 116 | + for ( i = 0; i < b.iterations; i++ ) { |
| 117 | + idx = i % 11; |
| 118 | + out = ind( idx, 10 ); |
| 119 | + if ( out !== out ) { |
| 120 | + b.fail( 'should not return NaN' ); |
| 121 | + } |
| 122 | + } |
| 123 | + b.toc(); |
| 124 | + if ( !isNonNegativeInteger( out ) ) { |
| 125 | + b.fail( 'should return a nonnegative integer' ); |
| 126 | + } |
| 127 | + b.pass( 'benchmark finished' ); |
| 128 | + b.end(); |
| 129 | +}); |
| 130 | + |
| 131 | +bench( pkg+':factory:mode=normalize', function benchmark( b ) { |
| 132 | + var ind; |
| 133 | + var out; |
| 134 | + var idx; |
| 135 | + var i; |
| 136 | + |
| 137 | + ind = factory( 'normalize' ); |
| 138 | + |
| 139 | + b.tic(); |
| 140 | + for ( i = 0; i < b.iterations; i++ ) { |
| 141 | + idx = (i%21) - 10; |
| 142 | + out = ind( idx, 10 ); |
| 143 | + if ( out !== out ) { |
| 144 | + b.fail( 'should not return NaN' ); |
| 145 | + } |
| 146 | + } |
| 147 | + b.toc(); |
| 148 | + if ( !isNonNegativeInteger( out ) ) { |
| 149 | + b.fail( 'should return a nonnegative integer' ); |
| 150 | + } |
| 151 | + b.pass( 'benchmark finished' ); |
| 152 | + b.end(); |
| 153 | +}); |
0 commit comments