declaration vs expression

JavaScript performance comparison

Revision 7 of this test case created by Jakeb

Preparation code

<script>
(function(global){
function E() {
 return (function() {
   return 42 * 42
 })()
}
function D() {
  return B();
  function B () {
    return 42 * 42
  }
}
function C (){
   return B()
}
function B () {
  return 42 * 42
}
global.closure = {
  internalExpression : E,
  internalDeclaration : D,
  externalDeclaration : C
}
})(window)
function E() {
 return (function() {
   return 42 * 42
 })()
}
function D() {
  return B();
  function B () {
    return 42 * 42
  }
}
function C (){
   return B()
}
function B () {
  return 42 * 42
}
window.withoutClosure = {
  internalExpression : E,
  internalDeclaration : D,
  externalDeclaration : C
}
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
1 - local expression
closure.internalExpression()
pending…
1 - local declaration
closure.internalDeclaration()
pending…
1 - Not internally declared function
closure.externalDeclaration()
pending…
2 - Not internally declared function
withoutClosure.externalDeclaration()
pending…
2 - local declaration
withoutClosure.internalDeclaration()
pending…
2 - local expression
withoutClosure.internalExpression()
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

2 comments

Jakeb (revision owner) commented :

I guess internal functions aren't stored in memory like I had assumed they were. Guess that means I've learned a new way to save memory

Howard commented :

Very interesting. Thank you.

Add a comment