Immediately-Invoked Function Expression (IIFE) Patterns in JS (v4)

Revision 4 of this benchmark created on


Description


Each of these represents one way of instantiating an IIFE (immediately-invoked function expression, AKA self-invoking function) in JS. (Learn more about IIFEs here.)

» They appear in descending order of popularity.

» The first several use symbolic operators, while the latter several use keywords and statements.

ck

Test runner

Ready to run.

Testing in
TestOps/sec
Expressive Create-Instance (())
(function() {})(); // returns implicit undefined

(function() {
  return true;
})(); // returns explicit true

(function(x) {
  return x;
})('FOO'); // returns implicit undefined
ready
Bitwise NOT (~)
~
function() {}(); // returns implicit -1

~

function() {
  return true;
}(); // returns explicit -2

~

function(x) {
  return x;
}('FOO'); // returns implicit -1
ready
Bang/Logical Inversion (!)
! function() {}(); // returns implicit true

! function() {
  return true;
}(); // returns explicit false

! function(x) {
  return x;
}('FOO'); // returns implicit true
ready
Unary Sum (+)
+ function() {}(); // returns implicit NaN

+ function() {
  return true;
}(); // returns explicit 1

+ function(x) {
  return x;
}('FOO'); // returns explicit NaN
ready
Unary Negation (-)
- function() {}(); // returns explicit NaN

- function() {
  return true;
}(); // returns explicit -1

- function(x) {
  return x;
}('FOO'); // returns explicit NaN
ready
Forced Instantial Ternary (?:)
true ? function() {}() : null; // returns explicit undefined

'foo' ? function() {
  return true;
}() : null; // returns explicit true

'foo' ? function(x) {
  return x;
}('FOO') : null; // returns explicit undefined
ready
Forced Fallback Ternary (?:)
false ? null : function() {}(); // returns explicit undefined

false ? null : function() {
  return true;
}(); // returns explicit true

false ? null : function(x) {
  return x;
}('FOO'); // returns explicit undefined
ready
Indirect Clausal Evaluation by Comma (,)
'',
function() {}(); // returns explicit undefined

'',
function() {
  return true;
}(); // returns explicit true

'',
function(x) {
  return x;
}('FOO'); // returns explicit undefined
ready
Faux Constructor (new)
new function() {}(); // returns explicit [object Object]

new function() {
  return true;
}(); // returns implicit [object Object]

new function(x) {
  return x;
}('FOO'); // returns implicit [object Object]
ready
Void Operator (void)
void
function() {}(); // returns explicit undefined

void
function() {
  return true;
}(); // returns explicit undefined

void
function(x) {
  return x;
}('FOO'); // returns explicit undefined
ready
Contextual Return Statement (return)
return function() {}(); // returns explicit undefined

return function() {
  return true;
}(); // returns explicit true

return function(x) {
  return x;
}('FOO'); // returns explicit undefined
ready
Delete Operator (delete)
delete
function() {}(); // returns implicit true

delete
function() {
  return true;
}(); // returns implicit true

delete
function(x) {
  return x;
}('FOO'); // returns implicit true

/*
  NOTE: Return values may depend on environment’s adherence to specification. 
*/
ready
Contextual Throw Statement (throw)
throw
function() {}(); // returns falsey exception

throw
function() {
  return true;
}(); // returns true exception

throw
function(x) {
  return x;
}('FOO'); // returns falsey exception

/*
  NOTE: Yields an error in JSPerf, but actual performance is similar to Delete Operator method (above).
*/
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.