Skip to content

Commit 3c95a9e

Browse files
committed
Add benchmarks for accessing property values
1 parent fff4547 commit 3c95a9e

File tree

9 files changed

+1305
-0
lines changed

9 files changed

+1305
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
25+
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var Float32Array = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::get:buffer', function benchmark( b ) {
33+
var arr;
34+
var v;
35+
var i;
36+
37+
arr = new Float32Array( 2 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
// Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless...
42+
v = arr.buffer;
43+
if ( typeof v !== 'object' ) {
44+
b.fail( 'should return an object' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isArrayBuffer( v ) ) {
49+
b.fail( 'should return an ArrayBuffer' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( pkg+'::get:byteLength', function benchmark( b ) {
56+
var arr;
57+
var v;
58+
var i;
59+
60+
arr = new Float32Array( 2 );
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
// Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless...
65+
v = arr.byteLength;
66+
if ( v !== v ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isNonNegativeInteger( v ) ) {
72+
b.fail( 'should return a nonnegative integer' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
});
77+
78+
bench( pkg+'::get:byteOffset', function benchmark( b ) {
79+
var arr;
80+
var v;
81+
var i;
82+
83+
arr = new Float32Array( 2 );
84+
85+
b.tic();
86+
for ( i = 0; i < b.iterations; i++ ) {
87+
// Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless...
88+
v = arr.byteOffset;
89+
if ( v !== v ) {
90+
b.fail( 'should not return NaN' );
91+
}
92+
}
93+
b.toc();
94+
if ( !isNonNegativeInteger( v ) ) {
95+
b.fail( 'should return a nonnegative integer' );
96+
}
97+
b.pass( 'benchmark finished' );
98+
b.end();
99+
});
100+
101+
bench( pkg+'::get:BYTES_PER_ELEMENT', function benchmark( b ) {
102+
var arr;
103+
var v;
104+
var i;
105+
106+
arr = new Float32Array( 2 );
107+
108+
b.tic();
109+
for ( i = 0; i < b.iterations; i++ ) {
110+
// Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless...
111+
v = arr.BYTES_PER_ELEMENT;
112+
if ( v !== v ) {
113+
b.fail( 'should not return NaN' );
114+
}
115+
}
116+
b.toc();
117+
if ( !isNonNegativeInteger( v ) ) {
118+
b.fail( 'should return a nonnegative integer' );
119+
}
120+
b.pass( 'benchmark finished' );
121+
b.end();
122+
});
123+
124+
bench( pkg+'::get:length', function benchmark( b ) {
125+
var arr;
126+
var v;
127+
var i;
128+
129+
arr = new Float32Array( 2 );
130+
131+
b.tic();
132+
for ( i = 0; i < b.iterations; i++ ) {
133+
// Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless...
134+
v = arr.length;
135+
if ( v !== v ) {
136+
b.fail( 'should not return NaN' );
137+
}
138+
}
139+
b.toc();
140+
if ( !isNonNegativeInteger( v ) ) {
141+
b.fail( 'should return a nonnegative integer' );
142+
}
143+
b.pass( 'benchmark finished' );
144+
b.end();
145+
});
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
25+
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var Float64Array = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::get:buffer', function benchmark( b ) {
33+
var arr;
34+
var v;
35+
var i;
36+
37+
arr = new Float64Array( 2 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
// Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless...
42+
v = arr.buffer;
43+
if ( typeof v !== 'object' ) {
44+
b.fail( 'should return an object' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isArrayBuffer( v ) ) {
49+
b.fail( 'should return an ArrayBuffer' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( pkg+'::get:byteLength', function benchmark( b ) {
56+
var arr;
57+
var v;
58+
var i;
59+
60+
arr = new Float64Array( 2 );
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
// Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless...
65+
v = arr.byteLength;
66+
if ( v !== v ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isNonNegativeInteger( v ) ) {
72+
b.fail( 'should return a nonnegative integer' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
});
77+
78+
bench( pkg+'::get:byteOffset', function benchmark( b ) {
79+
var arr;
80+
var v;
81+
var i;
82+
83+
arr = new Float64Array( 2 );
84+
85+
b.tic();
86+
for ( i = 0; i < b.iterations; i++ ) {
87+
// Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless...
88+
v = arr.byteOffset;
89+
if ( v !== v ) {
90+
b.fail( 'should not return NaN' );
91+
}
92+
}
93+
b.toc();
94+
if ( !isNonNegativeInteger( v ) ) {
95+
b.fail( 'should return a nonnegative integer' );
96+
}
97+
b.pass( 'benchmark finished' );
98+
b.end();
99+
});
100+
101+
bench( pkg+'::get:BYTES_PER_ELEMENT', function benchmark( b ) {
102+
var arr;
103+
var v;
104+
var i;
105+
106+
arr = new Float64Array( 2 );
107+
108+
b.tic();
109+
for ( i = 0; i < b.iterations; i++ ) {
110+
// Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless...
111+
v = arr.BYTES_PER_ELEMENT;
112+
if ( v !== v ) {
113+
b.fail( 'should not return NaN' );
114+
}
115+
}
116+
b.toc();
117+
if ( !isNonNegativeInteger( v ) ) {
118+
b.fail( 'should return a nonnegative integer' );
119+
}
120+
b.pass( 'benchmark finished' );
121+
b.end();
122+
});
123+
124+
bench( pkg+'::get:length', function benchmark( b ) {
125+
var arr;
126+
var v;
127+
var i;
128+
129+
arr = new Float64Array( 2 );
130+
131+
b.tic();
132+
for ( i = 0; i < b.iterations; i++ ) {
133+
// Note: the following may be optimized away due to loop invariant code motion and/or other compiler optimizations, rendering this benchmark meaningless...
134+
v = arr.length;
135+
if ( v !== v ) {
136+
b.fail( 'should not return NaN' );
137+
}
138+
}
139+
b.toc();
140+
if ( !isNonNegativeInteger( v ) ) {
141+
b.fail( 'should return a nonnegative integer' );
142+
}
143+
b.pass( 'benchmark finished' );
144+
b.end();
145+
});

0 commit comments

Comments
 (0)