Skip to content

Instantly share code, notes, and snippets.

@uhop
Created August 12, 2011 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uhop/1141172 to your computer and use it in GitHub Desktop.
Save uhop/1141172 to your computer and use it in GitHub Desktop.
Compare ++, --, +=, -=, = +, and = -, with different values.
// Compare ++, --, +=, -=, = +, and = -, with different values.
var a = 1, p1 = 1, m1 = -1, p9 = 9, m9 = -9;
this.group(
"inc/dec by 1",
function baseline() { return a; },
function prefix_plus_plus() { ++a; return a; },
function postfix_plus_plus() { a++; return a; },
function prefix_minus_minus() { --a; return a; },
function postfix_minus_minus() { a--; return a; },
function plus_eq() { a += 1; return a; },
function minus_eq() { a -= -1; return a; },
function eq_plus() { a = a + 1; return a; },
function eq_minus() { a = a - (-1); return a; }
);
this.group(
"inc/dec by 1 (var)",
function plus_eq_v1() { a += p1; return a; },
function minus_eq_v1() { a -= m1; return a; },
function eq_plus_v1() { a = a + p1; return a; },
function eq_minus_v1() { a = a - m1; return a; }
);
this.group(
"inc/dec by 9 (var)",
function plus_eq_v9() { a += p9; return a; },
function minus_eq_v9() { a -= m9; return a; },
function eq_plus_v9() { a = a + p9; return a; },
function eq_minus_v9() { a = a - m9; return a; }
);
@uhop
Copy link
Author

uhop commented Aug 12, 2011

Benchmark it with http://www.perfjs.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment