synchronized-promise icon indicating copy to clipboard operation
synchronized-promise copied to clipboard

It's very slow for me.

Open axkibe opened this issue 1 year ago • 0 comments

This package is cool it makes something possible (in workaround situations) that should not be.. but from my initial tests it's very slow.

Following test case: (node v18.14.0)

const sp = require('synchronized-promise');
const a = async function( x ) { return x + 1; }
const b = function b( x ) { return x + 1; }
const n = 50;

console.time( 'simple sync');
let v = 0;
for( let i = 0; i < n; i++ )
{
	v = b( v );
}
console.timeEnd( 'simple sync');

console.time( 'synchornized promise');
const as = sp( a );
v = 0;
for( let i = 0; i < n; i++ )
{
	v = as( v );
}
console.timeEnd( 'synchornized promise');


console.time( 'classic async');
( async function test( )
{
	v = 0;
	for( let i = 0; i < n; i++ )
	{
		v = await a( v );
	}
	console.timeEnd( 'classic async');
}) ( );

classic async has to be last test, since the main loop would continue straight away.

Result:

$ node src/a.js 
simple sync: 0.044ms
synchornized promise: 5.022s
classic async: 0.136ms

As expected promises are a tad slower than direct calls.. but syncronized promises, is way out there in being extremly slow (the code generator I wanted to use it in, where I have an await edge case turns from being almost instant to .. wait a minute or two).

I'm okay with again some overhead to await/async, but this is a bit too much.

axkibe avatar Mar 06 '23 13:03 axkibe