Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript void performance

If my script executes a function that returns e.g. a huge object that I don't want to use or store, is it better/faster/less memory intensive to call that function with the void operator? Or will it decrease the performance because the return value will just be overwritten?

void myFunc();

Just created a test:

http://jsperf.com/voidperf

like image 640
headacheCoder Avatar asked Nov 04 '22 11:11

headacheCoder


1 Answers

The void operator is only used to obtain the undefined primitive value; it can be useful when you run code in javascript: inline mode, because the page won't get replaced by the return value (casted to string) if it's undefined.

Therefore, there's no appreciable difference between these two constructs:

getSuperBigResult();
void getSuperBigResult();

JavaScript will run both in a void'ish manner, because the former statement doesn't use the return value either.

See also: void

like image 110
Ja͢ck Avatar answered Nov 09 '22 12:11

Ja͢ck