I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...
Unless you are returning by reference, they mean the same thing. It is preferable to exclude the parentheses. From the docs:
ReplyDeleteNote: Note that since return() is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so as PHP has less work to do in this case.
Note: You should never use parentheses around your return variable when returning by reference, as this will not work. You can only return variables by reference, not the result of a statement. If you use return ($a); then you're not returning a variable, but the result of the expression ($a) (which is, of course, the value of $a).
From the PHP Manual:
ReplyDelete"You should never use parentheses around your return variable when returning by reference, as this will not work. You can only return variables by reference, not the result of a statement. If you use return ($a); then you're not returning a variable, but the result of the expression ($a) (which is, of course, the value of $a)."
Edit:
This means that there is a difference between return($var) and return $var. Specifically, the former will return the value of $var and the latter will return $var itself.
Note that since return() is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so as PHP has less work to do in this case.
ReplyDeleteYou should never use parentheses around your return variable when returning by reference, as this will not work. You can only return variables by reference, not the result of a statement. If you use return ($a); then you're not returning a variable, but the result of the expression ($a) (which is, of course, the value of $a).
(Source)
The only time I use parenthesis is when returning two values with an array.
ReplyDeletereturn array($val,$valb);
This example is different than your initial question, however this is the only instance where I think parenthesis are required.
Yes. If you use parenthesis it will become an expression that PHP will have to solve first, which is a waste of a few CPU cycles. Additionally, you cannot return by reference using return($var);, only by using return $var;
ReplyDeleteThis is all clearly stated in the manual.
It's a language construct, not a function, so both are working almost the same. There are two differences tho. Quoting php.net:
ReplyDeleteIf no parameter is supplied, then the
parentheses must be omitted and NULL
will be returned. Calling return()
with parentheses but with no arguments
will result in a parse error.
and
You should never use parentheses
around your return variable when
returning by reference, as this will
not work. You can only return
variables by reference, not the result
of a statement. If you use return
($a); then you're not returning a
variable, but the result of the
expression ($a) (which is, of course,
the value of $a).
Source: php.net
They're almost the same. In fact, the parentheses don't even apply to the return keyword, but instead to the return value. So instead of looking at it like this:
ReplyDeletereturn( passing $var as an argument )
PHP actually looks at it like this:
$ret = ($var);
return $ret;
The parentheses make PHP evaluate $var which does nothing but waste time, really.