This blog post has a really thorough discussion of the general concept.
Upon starting this question the term 'self executing' was what I knew as I've seen it in other contexts but during my research I discovered the term 'immediately invoked' which is more accurate since the execution occurs outside the function body. I've left 'self' in the question to aid others.
If you're curious about the why preceding with an operator works check this answer to 'Javascript Self Executing supposed to work?' which elaborates as well as this answer to 'Explain JavaScript's encapsulated anonymous function syntax' which also has useful information about encapsulating functions for immediate execution.
From a performance perspective this blog post 'Self-Invoking Functions' compares between !
,-
,+
,~
,void
,delete
operators and parens. The differences appear to be negligible so readability is probably preferred: as the authors puts it it is still recommended to use the parentheses or a bang [!] because they are programming conventions
.
One obvious effect of preceding with an operator is the way each operator will effect the return type, but if you are capturing the return value via assignment (which seems to be the only way with a immediate invocation) then you can omit an operator like !
since the assignment operator allows the parser to immediately execute the function: var capture = function(){ return 'capture this' }();
Another edge case is that you can use the new
keyword with parentheses but not when preceding with an operator — see this question 'Are “(function ( ) { } ) ( )” and “(function ( ) { } ( ) )” functionally equal in JavaScript?' which has an interesting answer revealing how the placement of the executing parentheses and the use of new
can change the behavior of the encapsulation, which (as stated) is not possible when preceding with an operator.