Skip to content

Commit 400fcbb

Browse files
committed
refactor: avoid unnecessary copies, update examples, and refactor benchmarks
1 parent 3dc813f commit 400fcbb

File tree

5 files changed

+86
-105
lines changed

5 files changed

+86
-105
lines changed

lib/node_modules/@stdlib/array/base/remove-at/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ var randi = require( '@stdlib/random/base/discrete-uniform' );
9090
var removeAt = require( '@stdlib/array/base/remove-at' );
9191

9292
// Create an array of random numbers:
93-
var x = discreteUniform( 30, 0, 5, {
93+
var x = discreteUniform( 10, 0, 5, {
9494
'dtype': 'generic'
9595
});
9696
// returns [...]
9797

98+
console.log( x );
99+
98100
// Remove a random element:
99101
var y = removeAt( x, randi( 0, x.length-1 ) );
100102
// returns [...]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var pkg = require( './../package.json' ).name;
27+
var removeAt = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':index=-1', function benchmark( b ) {
33+
var out;
34+
var x;
35+
var i;
36+
37+
x = uniform( b.iterations+1, 0.0, 10.0, {
38+
'dtype': 'generic'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
out = removeAt( x, -1 );
44+
if ( out.length === 0 ) {
45+
b.fail( 'unexpected length' );
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+':index=0', function benchmark( b ) {
57+
var out;
58+
var x;
59+
var i;
60+
61+
x = uniform( b.iterations+1, 0.0, 10.0, {
62+
'dtype': 'generic'
63+
});
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
out = removeAt( x, 0 );
68+
if ( out.length === 0 ) {
69+
b.fail( 'unexpected length' );
70+
}
71+
}
72+
b.toc();
73+
if ( !isArray( out ) ) {
74+
b.fail( 'should return an array' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
});

lib/node_modules/@stdlib/array/base/remove-at/benchmark/benchmark.length.js

Lines changed: 0 additions & 98 deletions
This file was deleted.

lib/node_modules/@stdlib/array/base/remove-at/examples/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ var randi = require( '@stdlib/random/base/discrete-uniform' );
2323
var removeAt = require( './../lib' );
2424

2525
// Create an array of random numbers:
26-
var x = discreteUniform( 30, 0, 5, {
26+
var x = discreteUniform( 10, 0, 5, {
2727
'dtype': 'generic'
2828
});
2929
// returns [...]
3030

31+
console.log( x );
32+
3133
// Remove a random element:
3234
var y = removeAt( x, randi( 0, x.length-1 ) );
3335
// returns [...]

lib/node_modules/@stdlib/array/base/remove-at/lib/main.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ function removeAt( x, index ) {
5050
} else if ( index >= len ) {
5151
return x;
5252
}
53-
j = 0;
54-
for ( i = 0; i < len; i++ ) {
55-
if ( i === index ) {
56-
continue;
57-
}
53+
j = index;
54+
for ( i = index+1; i < len; i++ ) {
5855
x[ j ] = x[ i ];
5956
j += 1;
6057
}

0 commit comments

Comments
 (0)