Is Subtracting Zero some kind of JavaScript performance trick?

In jQuery core, I found the following code convention:

nth: function(elem, i, match){ return match[3] - 0 === i; }, 

And I really liked the match[3] - 0 snippet

Googling "-0" on google is not very productive, and a search for "minus zero" returns a link to Bob Dylan's song.

So can someone tell me. Is this some kind of performance trick, or is there a reason for this rather than parseInt or parseFloat ?

+18
javascript jquery
Apr 19 2018-10-10T00:
source share
7 answers

Based on a few quick and dirty tests, "1234" - 0 was about 50% faster than parseInt("1234") and 10% faster than +"1234" in Firefox 3.6.

Update:

My "quick and dirty" test was not very useful because it just converted the string "1234" in a loop. I tried again using a random list of numbers, and the results are all on the map. Three methods are within 400-500 ms on this computer, unless they skip to 1300 ms! I think garbage collection is interfering. Here is the code to play Firebug if I did something stupid:

 function randomList() { var list = []; for (var i = 0; i < 1000000; i++) { list.push("" + Math.floor(Math.random()*4000000000)); } return list; } function testParseInt(list) { console.log("Sanity check: parseInt('" + list[0] + "') = " + parseInt(list[0]) ); var start = new Date(); for (var string in list) var tmp = parseInt(string); var time = new Date() - start; console.log("parseInt(string): " + time); } function testMinusZero(list) { console.log("Sanity check: '" + list[0] + "' - 0 = " + (list[0] - 0)); var start = new Date(); for (var string in list) var tmp = string - 0; var time = new Date() - start; console.log("string - 0: " + time); } function testUnaryPlus(list) { console.log("Sanity check: +'" + list[0] + "' = " + (+list[0])); var start = new Date(); for (var string in list) var tmp = +string; var time = new Date() - start; console.log("+string: " + time); } function testPlusZero(list) { console.log("Sanity check: '" + list[0] + "' + 0 = " + (list[0] + 0) + " Oh no!"); var start = new Date(); for (var string in list) var tmp = string + 0; var time = new Date() - start; console.log("string + 0: " + time); } var numbers = randomList(); testParseInt(numbers); testMinusZero(numbers); testUnaryPlus(numbers); testPlusZero(numbers); 
+9
Apr 19 '10 at 8:10
source share

This is probably just a short way to force the left side into a whole. Not as clear as a function call, of course.

This type conversion guide states:

Any mathematical operator except the concatenation / addition operator will be a forced type conversion. So, converting a string to a number can entail performing a mathematical operation on the string representation of a number that does not affect the resulting number, for example, subtracting zero or multiplying by one.

It also shows that "subtraction" is a better search term than minus. :)

+14
Apr 19 2018-10-10T00:
source share

Different ways to cast JS strings to numbers and their consequences:

Results of converting various strings using the above techniques
(source: phrogz.net )

I personally use *1 , since it is short for input, but it still stands out (unlike unary +), and either gives me what the user typed in or fails. I use parseInt() only when I know that there will be non-numeric content at the end that I need to ignore, or when I need to parse a non-base-10 string.

+10
Jan 05 2018-11-11T00:
source share

Just information according to this site

using the unary + operator is faster than one of the following (including "- 0"):

 var numValue = stringValue - 0; /* or */ var numValue = stringValue * 1; /* or */ var numValue = stringValue / 1; 

The unary + operator also converts the type of its operand to a number, and because it does not perform any additional mathematical operations, this is the fastest method for converting the string type to a number.

This contradicts James’s criterion, although he may be right. I think jQuery would not use this syntax if it were slow.

+5
Apr 19 '10 at 8:31
source share

The main reason for using this syntax is that you have common code that can be any number (int or float), and you want to do a type comparison (===)

+3
Apr 19 2018-10-10T00:
source share

If this is not an old relic that is lost, then it just tries to change the type to Number.

0
Apr 19 '10 at 8:07
source share

It really looks like a parseInt "performer" to me.

0
Apr 19 '10 at 8:08
source share



All Articles