Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is js() a function instead of a variable? #5

Open
marcoos opened this issue May 23, 2011 · 5 comments
Open

Why is js() a function instead of a variable? #5

marcoos opened this issue May 23, 2011 · 5 comments

Comments

@marcoos
Copy link

marcoos commented May 23, 2011

Shouldn't this:

function js() {
 return ("00"==false);
}

be replaced with this:

$js = ("00" == false);

This way, there would be less function calls and the "00"==false expression would only be evaluated once per script run.

@tantek
Copy link
Owner

tantek commented May 25, 2011

Two reasons offhand:

  1. to avoid dependency on global variables (especially in core functionality!)
  2. jitted/incremental interpreters (both JS & PHP) will cache the result of evaluating the function as always a fixed constant and thus from a performance perspective the result should be the same.

If you find actual performance differences/issues with the use of such a constant function, I'd be interested in seeing them.

@tantek tantek closed this as completed May 25, 2011
@marcoos
Copy link
Author

marcoos commented May 25, 2011

  1. Technically, in JS a "global function" is a global variable.

  2. That depends on the JIT, but I've done a JSPerf test just a moment ago: http://jsperf.com/cassis-fun-vs-var

In Firefox the variable is almost three times faster than function call, in Safari and Opera - almost twenty times faster. Or am I doing the test wrong? :)

@tantek tantek reopened this May 25, 2011
@tantek
Copy link
Owner

tantek commented May 25, 2011

I just verified similar results with your test on my MacBook Air 11". That's quite the difference. Ok, will consider a change like this for v0.2 (working hard to wrap up last changes for v0.1). In particular I may use a slightly longer name (maybe $in_js) for the global variable to reduce likelihood of collision.

@tantek tantek closed this as completed May 25, 2011
@tantek tantek reopened this May 25, 2011
@aaronpk
Copy link
Collaborator

aaronpk commented Sep 13, 2015

I don't think this will work as a variable, since in PHP, global variables must be declared at the beginning of the function. For example, in order for a function to be able to see the $in_js variable, it would need to look like this:

function example() {
  global $in_js;
  if($in_js) {
    // javascript
  } else {
    // php
  }
}

The advantage of using the js() function is that functions are available in the global scope.

@tantek
Copy link
Owner

tantek commented Nov 3, 2016

Just switched to returning true or false depending on which comment-code path so that should be even faster (for any modern js jit / or PHP processors).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants