Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is unrelated to this PR.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */
/* eslint-disable no-undefined, no-empty-function */

'use strict';

Expand Down
52 changes: 52 additions & 0 deletions lib/node_modules/@stdlib/utils/async/any-by/internal/instrument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

// MAIN //

/**
* Creates a high-resolution timer to measure async operations.
*
* @returns {Function} function to stop the timer and return the duration in milliseconds
*/
function createTimer() {
var start = process.hrtime();

return stop;

/**
* Stops the timer and returns the elapsed time.
*
* @private
* @returns {number} elapsed time in milliseconds
*/
function stop() {
var diff = process.hrtime( start );

// Convert to milliseconds:
return ( diff[ 0 ] * 1e3 ) + ( diff[ 1 ] / 1e6 );
}
}


// EXPORTS //

module.exports = createTimer;
8 changes: 6 additions & 2 deletions lib/node_modules/@stdlib/utils/async/any-by/lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var format = require( '@stdlib/string/format' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var validate = require( './validate.js' );
var limit = require( './limit.js' );
var createTimer = require( './../internal/instrument.js' );


// MAIN //
Expand Down Expand Up @@ -129,6 +130,8 @@ function factory( options, predicate ) {
* @returns {void}
*/
function anyByAsync( collection, done ) {
var timer = createTimer();

if ( !isCollection( collection ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );
}
Expand All @@ -146,10 +149,11 @@ function factory( options, predicate ) {
* @returns {void}
*/
function clbk( error, bool ) {
var duration = timer();
if ( error ) {
return done( error, false );
return done( error, false, duration );
}
done( null, bool );
done( null, bool, duration );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var tape = require( 'tape' );
var createTimer = require( './../internal/instrument.js' );


// TESTS //

tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.strictEqual( typeof createTimer, 'function', 'main export is a function' );
t.end();
});

tape( 'the function returns a function', function test( t ) {
var stop = createTimer();

t.strictEqual( typeof stop, 'function', 'returns a function' );
t.end();
});

tape( 'the returned function returns a number (elapsed milliseconds)', function test( t ) {
var duration;
var stop = createTimer();

setTimeout( onTimeout, 10 );

function onTimeout() {
duration = stop();

t.strictEqual( typeof duration, 'number', 'returns a number' );
t.ok( duration >= 0.0, 'returns a non-negative number' );
t.end();
}
});