|
| 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 randu = require( '@stdlib/random/base/randu' ); |
| 25 | +var Int8Array = require( '@stdlib/array/int8' ); |
| 26 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 27 | +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); |
| 28 | +var Int16Array = require( '@stdlib/array/int16' ); |
| 29 | +var Uint16Array = require( '@stdlib/array/uint16' ); |
| 30 | +var Int32Array = require( '@stdlib/array/int32' ); |
| 31 | +var Uint32Array = require( '@stdlib/array/uint32' ); |
| 32 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 33 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 34 | +var pkg = require( './../package.json' ).name; |
| 35 | +var copy = require( './../lib' ); |
| 36 | + |
| 37 | + |
| 38 | +// MAIN // |
| 39 | + |
| 40 | +bench( pkg+':level=+infinity', function benchmark( b ) { |
| 41 | + var values; |
| 42 | + var out; |
| 43 | + var val; |
| 44 | + var i; |
| 45 | + |
| 46 | + values = [ |
| 47 | + [ |
| 48 | + { |
| 49 | + 'x': new Date(), |
| 50 | + 'y': [ randu(), randu() ], |
| 51 | + 'z': new Int32Array( [ 1, 2, 3, 4 ] ), |
| 52 | + 'label': 'Beep' |
| 53 | + }, |
| 54 | + { |
| 55 | + 'x': new Date(), |
| 56 | + 'y': [ randu(), randu() ], |
| 57 | + 'z': new Int32Array( [ 3, 1, 2, 4 ] ), |
| 58 | + 'label': 'Boop' |
| 59 | + } |
| 60 | + ], |
| 61 | + null, |
| 62 | + void 0, |
| 63 | + NaN, |
| 64 | + 3.14, |
| 65 | + 'boop', |
| 66 | + new Int8Array( [ 1, 2, 3, 4 ] ), |
| 67 | + new Uint8Array( [ 5, 6, 7, 8 ] ), |
| 68 | + new Uint8ClampedArray( [ 9, 10, 11, 12 ] ), |
| 69 | + new Int16Array( [ 256, 257, 258 ] ), |
| 70 | + new Uint16Array( [ 259, 260, 261 ] ), |
| 71 | + new Int32Array( [ 65537, 65538 ] ), |
| 72 | + new Uint32Array( [ 65539, 65540 ] ), |
| 73 | + new Float32Array( [ 3.14 ] ), |
| 74 | + new Float64Array( [ 3.14 ] ), |
| 75 | + new Error( 'beep' ) |
| 76 | + ]; |
| 77 | + b.tic(); |
| 78 | + for ( i = 0; i < b.iterations; i++ ) { |
| 79 | + val = values[ i % values.length ]; |
| 80 | + out = copy( val ); |
| 81 | + if ( typeof out !== typeof val ) { |
| 82 | + b.fail( 'returns value of expected type' ); |
| 83 | + } |
| 84 | + } |
| 85 | + b.toc(); |
| 86 | + if ( typeof out !== typeof val ) { |
| 87 | + b.fail( 'returns value of expected type' ); |
| 88 | + } |
| 89 | + if ( out === val ) { |
| 90 | + b.fail( 'returns a copy of the input value' ); |
| 91 | + } |
| 92 | + b.pass( 'benchmark finished' ); |
| 93 | + b.end(); |
| 94 | +}); |
| 95 | + |
| 96 | +bench( pkg+':level=1', function benchmark( b ) { |
| 97 | + var values; |
| 98 | + var out; |
| 99 | + var val; |
| 100 | + var i; |
| 101 | + |
| 102 | + values = [ |
| 103 | + [ |
| 104 | + { |
| 105 | + 'x': new Date(), |
| 106 | + 'y': [ randu(), randu() ], |
| 107 | + 'z': new Int32Array( [ 1, 2, 3, 4 ] ), |
| 108 | + 'label': 'Beep' |
| 109 | + }, |
| 110 | + { |
| 111 | + 'x': new Date(), |
| 112 | + 'y': [ randu(), randu() ], |
| 113 | + 'z': new Int32Array( [ 3, 1, 2, 4 ] ), |
| 114 | + 'label': 'Boop' |
| 115 | + } |
| 116 | + ], |
| 117 | + null, |
| 118 | + void 0, |
| 119 | + NaN, |
| 120 | + 3.14, |
| 121 | + 'boop', |
| 122 | + new Int8Array( [ 1, 2, 3, 4 ] ), |
| 123 | + new Uint8Array( [ 5, 6, 7, 8 ] ), |
| 124 | + new Uint8ClampedArray( [ 9, 10, 11, 12 ] ), |
| 125 | + new Int16Array( [ 256, 257, 258 ] ), |
| 126 | + new Uint16Array( [ 259, 260, 261 ] ), |
| 127 | + new Int32Array( [ 65537, 65538 ] ), |
| 128 | + new Uint32Array( [ 65539, 65540 ] ), |
| 129 | + new Float32Array( [ 3.14 ] ), |
| 130 | + new Float64Array( [ 3.14 ] ), |
| 131 | + new Error( 'beep' ) |
| 132 | + ]; |
| 133 | + b.tic(); |
| 134 | + for ( i = 0; i < b.iterations; i++ ) { |
| 135 | + val = values[ i % values.length ]; |
| 136 | + out = copy( val, 1 ); |
| 137 | + if ( typeof out !== typeof val ) { |
| 138 | + b.fail( 'returns value of expected type' ); |
| 139 | + } |
| 140 | + } |
| 141 | + b.toc(); |
| 142 | + if ( typeof out !== typeof val ) { |
| 143 | + b.fail( 'returns value of expected type' ); |
| 144 | + } |
| 145 | + if ( out === val ) { |
| 146 | + b.fail( 'returns a copy of the input value' ); |
| 147 | + } |
| 148 | + b.pass( 'benchmark finished' ); |
| 149 | + b.end(); |
| 150 | +}); |
| 151 | + |
| 152 | +bench( pkg+':level=0', function benchmark( b ) { |
| 153 | + var values; |
| 154 | + var out; |
| 155 | + var val; |
| 156 | + var i; |
| 157 | + |
| 158 | + values = [ |
| 159 | + [ |
| 160 | + { |
| 161 | + 'x': new Date(), |
| 162 | + 'y': [ randu(), randu() ], |
| 163 | + 'z': new Int32Array( [ 1, 2, 3, 4 ] ), |
| 164 | + 'label': 'Beep' |
| 165 | + }, |
| 166 | + { |
| 167 | + 'x': new Date(), |
| 168 | + 'y': [ randu(), randu() ], |
| 169 | + 'z': new Int32Array( [ 3, 1, 2, 4 ] ), |
| 170 | + 'label': 'Boop' |
| 171 | + } |
| 172 | + ], |
| 173 | + null, |
| 174 | + void 0, |
| 175 | + NaN, |
| 176 | + 3.14, |
| 177 | + 'boop', |
| 178 | + new Int8Array( [ 1, 2, 3, 4 ] ), |
| 179 | + new Uint8Array( [ 5, 6, 7, 8 ] ), |
| 180 | + new Uint8ClampedArray( [ 9, 10, 11, 12 ] ), |
| 181 | + new Int16Array( [ 256, 257, 258 ] ), |
| 182 | + new Uint16Array( [ 259, 260, 261 ] ), |
| 183 | + new Int32Array( [ 65537, 65538 ] ), |
| 184 | + new Uint32Array( [ 65539, 65540 ] ), |
| 185 | + new Float32Array( [ 3.14 ] ), |
| 186 | + new Float64Array( [ 3.14 ] ), |
| 187 | + new Error( 'beep' ) |
| 188 | + ]; |
| 189 | + b.tic(); |
| 190 | + for ( i = 0; i < b.iterations; i++ ) { |
| 191 | + val = values[ i % values.length ]; |
| 192 | + out = copy( val, 0 ); |
| 193 | + if ( typeof out !== typeof val ) { |
| 194 | + b.fail( 'returns value of expected type' ); |
| 195 | + } |
| 196 | + } |
| 197 | + b.toc(); |
| 198 | + if ( typeof out !== typeof val ) { |
| 199 | + b.fail( 'returns value of expected type' ); |
| 200 | + } |
| 201 | + if ( out !== val ) { |
| 202 | + b.fail( 'returns input value' ); |
| 203 | + } |
| 204 | + b.pass( 'benchmark finished' ); |
| 205 | + b.end(); |
| 206 | +}); |
0 commit comments