Skip to content

Commit a7add6a

Browse files
committed
refactor: avoid unnecessary concat, fix benchmark, and clean-up
Ref: #1463 Ref: bb7817f
1 parent 0a6cefd commit a7add6a

File tree

8 files changed

+164
-81
lines changed

8 files changed

+164
-81
lines changed

lib/node_modules/@stdlib/array/base/join/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ var out = join( x, ',' );
6767
x.join( separator )
6868
```
6969
70+
- If provided an array-like object without a `join` method, the function manually constructs the output string.
71+
7072
- If an array element is either `null` or `undefined`, the function will serialize the element as an empty string.
7173
7274
</section>
@@ -99,8 +101,8 @@ s = join( x, ',' );
99101
s = join( x, '-' );
100102
// returns '0-1-2-3-4-5'
101103
102-
s = new AccessorArray( [ 1, 2, 3, 4 ] );
103-
s = join( s, ',' );
104+
x = new AccessorArray( [ 1, 2, 3, 4 ] );
105+
s = join( x, ',' );
104106
// returns '1,2,3,4'
105107
106108
x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );

lib/node_modules/@stdlib/array/base/join/benchmark/benchmark.length.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var pow = require( '@stdlib/math/base/special/pow' );
25-
var isString = require( '@stdlib/assert/is-string' );
25+
var isString = require( '@stdlib/assert/is-string' ).isrPrimitive;
2626
var ones = require( '@stdlib/array/base/ones' );
2727
var pkg = require( './../package.json' ).name;
2828
var join = require( './../lib' );
@@ -58,10 +58,10 @@ function createBenchmark( len ) {
5858
b.fail( 'should return a string' );
5959
}
6060
}
61+
b.toc();
6162
if ( !isString( s ) ) {
6263
b.fail( 'should return a string' );
6364
}
64-
b.toc();
6565
b.pass( 'benchmark finished' );
6666
b.end();
6767
}

lib/node_modules/@stdlib/array/base/join/docs/repl.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
{{alias}}( x, separator )
3-
Return a string created by joining array elements using a
4-
specified separator.
3+
Return a string created by joining array elements using a specified
4+
separator.
55

6-
If provided an array-like object having a `join` method, the function
7-
defers execution to that method and assumes that the method has the
8-
following signature:
6+
If provided an array-like object having a `join` method, the function defers
7+
execution to that method and assumes that the method has the following
8+
signature:
99

1010
x.join( separator )
1111

@@ -18,12 +18,12 @@
1818
Input array.
1919

2020
separator: string
21-
Separator to be used.
21+
Separator.
2222

2323
Returns
2424
-------
2525
out: string
26-
Joined string.
26+
Output string.
2727

2828
Examples
2929
--------

lib/node_modules/@stdlib/array/base/join/docs/types/index.d.ts

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { Collection, TypedArray, ComplexTypedArray } from '@stdlib/types/array';
23+
import { Collection, AccessorArrayLike } from '@stdlib/types/array';
2424

2525
/**
2626
* Returns a string created by joining array elements using a specified separator.
@@ -30,43 +30,34 @@ import { Collection, TypedArray, ComplexTypedArray } from '@stdlib/types/array';
3030
* @returns string
3131
*
3232
* @example
33-
* var Float64Array = require( '@stdlib/array/float64' );
34-
*
35-
* var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
33+
* var x = [ 1, 2, 3 ];
3634
*
3735
* var out = join( x, ',' );
3836
* // returns '1,2,3'
3937
*
4038
* @example
41-
* var Complex128Array = require( '@stdlib/array/complex128' );
42-
*
43-
* var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
44-
*
45-
* var out = join( x, ',' );
46-
* // returns '1 + 2i,3 + 4i,5 + 6i'
47-
*/
48-
declare function join<T extends TypedArray | ComplexTypedArray>( x: T, separator: string ): string;
49-
50-
/**
51-
* Returns a string created by joining array elements using a specified separator.
39+
* var x = [ 1, 2, 3, 4, 5, 6 ];
5240
*
53-
* @param x - input array
54-
* @param separator - separator element
55-
* @returns string
41+
* var out = join( x, '-' );
42+
* // returns '1-2-3-4-5-6'
5643
*
5744
* @example
58-
* var x = [ 1, 2, 3 ];
45+
* var Float64Array = require( '@stdlib/array/float64' );
46+
*
47+
* var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
5948
*
6049
* var out = join( x, ',' );
6150
* // returns '1,2,3'
6251
*
6352
* @example
64-
* var x = [ 1, 2, 3, 4, 5, 6 ];
53+
* var Complex128Array = require( '@stdlib/array/complex128' );
6554
*
66-
* var out = join( x, '-' );
67-
* // returns '1-2-3-4-5-6'
55+
* var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
56+
*
57+
* var out = join( x, ',' );
58+
* // returns '1 + 2i,3 + 4i,5 + 6i'
6859
*/
69-
declare function join<T = unknown>( x: Collection<T>, separator: string ): string;
60+
declare function join<T = unknown>( x: Collection<T> | AccessorArrayLike<T>, separator: string ): string;
7061

7162

7263
// EXPORTS //

lib/node_modules/@stdlib/array/base/join/docs/types/test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import join = require( './index' );
5353
{
5454
const x = [ 1, 2, 3 ];
5555

56+
join( x, 5 ); // $ExpectError
5657
join( x, true ); // $ExpectError
5758
join( x, false ); // $ExpectError
5859
join( x, null ); // $ExpectError

lib/node_modules/@stdlib/array/base/join/examples/index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,15 @@
2121
var Complex128Array = require( '@stdlib/array/complex128' );
2222
var Complex64Array = require( '@stdlib/array/complex64' );
2323
var Float64Array = require( '@stdlib/array/float64' );
24-
var zeroTo = require( '@stdlib/array/base/zero-to' );
2524
var AccessorArray = require( '@stdlib/array/base/accessor' );
2625
var join = require( './../lib' );
2726

2827
var x = [ 0, 1, 2, 3, 4, 5 ];
29-
3028
var s = join( x, ',' );
3129
console.log( s );
3230
// => '0,1,2,3,4,5'
3331

34-
x = new Float64Array( zeroTo( 6 ) );
35-
32+
x = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ] );
3633
s = join( x, ',' );
3734
console.log( s );
3835
// => '0,1,2,3,4,5'
@@ -41,20 +38,17 @@ s = join( x, '-' );
4138
console.log( s );
4239
// => '0-1-2-3-4-5'
4340

44-
s = new AccessorArray( [ 1, 2, 3, 4 ] );
45-
46-
s = join( s, ',' );
41+
x = new AccessorArray( [ 1, 2, 3, 4 ] );
42+
s = join( x, ',' );
4743
console.log( s );
4844
// => '1,2,3,4'
4945

5046
x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
51-
5247
s = join( x, ',' );
5348
console.log( s );
5449
// => '1 + 2i,3 + 4i,5 + 6i'
5550

5651
x = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0 ] );
57-
5852
s = join( x, ',' );
5953
console.log( s );
6054
// => '1 - 1i,2 - 2i'

lib/node_modules/@stdlib/array/base/join/lib/main.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// MODULES //
2222

2323
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
24+
var isUndefinedOrNull = require( '@stdlib/assert/is-undefined-or-null' );
2425

2526

2627
// FUNCTIONS //
@@ -46,12 +47,12 @@ function hasMethod( obj, method ) {
4647
}
4748

4849
/**
49-
* Returns a string created by joining array elements using a specified separator when input is an accessor array.
50+
* Returns a string created by joining elements in an accessor array using a specified separator.
5051
*
5152
* @private
5253
* @param {Object} x - input array object
5354
* @param {integer} separator - separator
54-
* @returns {string} joined string
55+
* @returns {string} output string
5556
*
5657
* @example
5758
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
@@ -63,25 +64,28 @@ function hasMethod( obj, method ) {
6364
* // returns '1,2,3,4'
6465
*/
6566
function accessors( x, separator ) {
66-
var output;
6767
var data;
68+
var out;
6869
var get;
69-
var i;
70+
var N;
7071
var v;
72+
var i;
73+
7174
data = x.data;
7275
get = x.accessors[ 0 ];
73-
output = '';
74-
for ( i = 0; i < data.length; i++ ) {
76+
77+
N = data.length - 1;
78+
out = '';
79+
for ( i = 0; i <= N; i++ ) {
7580
v = get( data, i );
76-
if ( typeof v === 'undefined' || v === null ) {
77-
v = '';
81+
if ( !isUndefinedOrNull( v ) ) {
82+
out += String( v );
7883
}
79-
output += v;
80-
if ( i < data.length - 1 ) {
81-
output += separator;
84+
if ( i < N ) {
85+
out += separator;
8286
}
8387
}
84-
return output;
88+
return out;
8589
}
8690

8791
/**
@@ -90,29 +94,32 @@ function accessors( x, separator ) {
9094
* @private
9195
* @param {Object} x - input array object
9296
* @param {integer} separator - separator
93-
* @returns {string} joined string
97+
* @returns {string} output string
9498
*
9599
* @example
96100
* var x = [ 1, 2, 3, 4 ];
97-
* var out = constructString( x, ',' );
101+
*
102+
* var out = indexed( x, ',' );
98103
* // returns '1,2,3,4'
99104
*/
100-
function constructString( x, separator ) {
101-
var i;
102-
var s;
105+
function indexed( x, separator ) {
106+
var out;
107+
var N;
103108
var v;
104-
s = '';
105-
for ( i = 0; i < x.length; i++ ) {
109+
var i;
110+
111+
N = x.length - 1;
112+
out = '';
113+
for ( i = 0; i <= N; i++ ) {
106114
v = x[ i ];
107-
if ( typeof v === 'undefined' || v === null ) {
108-
v = '';
115+
if ( !isUndefinedOrNull( v ) ) {
116+
out += String( v );
109117
}
110-
s += v;
111-
if ( i < x.length - 1 ) {
112-
s += separator;
118+
if ( i < N ) {
119+
out += separator;
113120
}
114121
}
115-
return s;
122+
return out;
116123
}
117124

118125

@@ -122,8 +129,8 @@ function constructString( x, separator ) {
122129
* Returns a string created by joining array elements using a specified separator.
123130
*
124131
* @param {Collection} x - input array
125-
* @param {integer} separator - separator to be used in string
126-
* @returns {string} joined string
132+
* @param {integer} separator - separator
133+
* @returns {string} output string
127134
*
128135
* @example
129136
* var x = [ 1, 2, 3, 4 ];
@@ -132,7 +139,7 @@ function constructString( x, separator ) {
132139
* // returns '1,2,3,4'
133140
*
134141
* @example
135-
* var x = [ 1, 2, 3, null, undefined, 4 ];
142+
* var x = [ 1, 2, 3, null, undefined, 4 ];
136143
*
137144
* var out = join( x, '-' );
138145
* // returns '1-2-3---4'
@@ -146,10 +153,7 @@ function join( x, separator ) {
146153
if ( obj.accessorProtocol ) {
147154
return accessors( obj, separator );
148155
}
149-
if ( obj.dtype === 'generic' || obj.dtype === null ) {
150-
return constructString( x, separator );
151-
}
152-
return x.join( separator );
156+
return indexed( x, separator );
153157
}
154158

155159

0 commit comments

Comments
 (0)