Skip to main content
Added to section on property access
Source Link
Riker
  • 7.9k
  • 4
  • 40
  • 73
for ( a; b; c ) 
for ( a; b; c ) 
a; while ( b ) { ... c; } 
a; while ( b ) { ... c; } 
for(x=y=n;!z;x--,y++)z=i(x)?x:i(y)?y:0 for(a=b=1;b<n;c=a+b,a=b,b=c); 
for(x=y=n;!z;x--,y++)z=i(x)?x:i(y)?y:0 for(a=b=1;b<n;c=a+b,a=b,b=c); 
a=b=1; 
a=b=1; 
a='30'; b='10'; c = a + b; //failure c = parseInt(a) + parseInt(b) //too long c = -(-a-b); //try these c = ~~a+~~b; c = +a+ +b; c = a- -b; 
a='30'; b='10'; c = a + b; //failure c = parseInt(a) + parseInt(b) //too long c = -(-a-b); //try these c = ~~a+~~b; c = +a+ +b; c = a- -b; 
a( realParam1, realParam2, fizz='buzz' ) 
a( realParam1, realParam2, fizz='buzz' ) 
a = a - 1; foo(a); 
a = a - 1; foo(a); 
foo(a); a = a - 1; 
foo(a); a = a - 1; 
foo(--a); 
foo(--a); 
foo(a--); 
foo(a--); 
a.longFunctionName(b) a.longFunctionName(c) //42 
a.longFunctionName(b) a.longFunctionName(c) //42 
a[f='longFunctionName'](b) a[f](c) //34 
a[f='longFunctionName'](b) a[f](c) //34 
len | i ======== 1 | ∞ 2 | ∞ 3 | 7 4 | 4 5 | 3 6 | 3 7 | 3 8+ | 2 
len | i ======== 1 | ∞ 2 | ∞ 3 | 7 4 | 4 5 | 3 6 | 3 7 | 3 8+ | 2 
for ( a; b; c ) 
a; while ( b ) { ... c; } 
for(x=y=n;!z;x--,y++)z=i(x)?x:i(y)?y:0 for(a=b=1;b<n;c=a+b,a=b,b=c); 
a=b=1; 
a='30'; b='10'; c = a + b; //failure c = parseInt(a) + parseInt(b) //too long c = -(-a-b); //try these c = ~~a+~~b; c = +a+ +b; c = a- -b; 
a( realParam1, realParam2, fizz='buzz' ) 
a = a - 1; foo(a); 
foo(a); a = a - 1; 
foo(--a); 
foo(a--); 
a.longFunctionName(b) a.longFunctionName(c) //42 
a[f='longFunctionName'](b) a[f](c) //34 
len | i ======== 1 | ∞ 2 | ∞ 3 | 7 4 | 4 5 | 3 6 | 3 7 | 3 8+ | 2 
for ( a; b; c ) 
a; while ( b ) { ... c; } 
for(x=y=n;!z;x--,y++)z=i(x)?x:i(y)?y:0 for(a=b=1;b<n;c=a+b,a=b,b=c); 
a=b=1; 
a='30'; b='10'; c = a + b; //failure c = parseInt(a) + parseInt(b) //too long c = -(-a-b); //try these c = ~~a+~~b; c = +a+ +b; c = a- -b; 
a( realParam1, realParam2, fizz='buzz' ) 
a = a - 1; foo(a); 
foo(a); a = a - 1; 
foo(--a); 
foo(a--); 
a.longFunctionName(b) a.longFunctionName(c) //42 
a[f='longFunctionName'](b) a[f](c) //34 
len | i ======== 1 | ∞ 2 | ∞ 3 | 7 4 | 4 5 | 3 6 | 3 7 | 3 8+ | 2 

###Use Array-Accessbracket notation for repeat function callsproperty access

This is definitely a balancing act between variable/functionproperty name length and number of invocationsaccesses. Instead of calling a.longFunctionName() with dot notation twice, it's shorter to save the name and call the function via array-accessbracket notation:

f='longFunctionName' a[f]a[f='longFunctionName'](b) a[f](c) //34 

this is especially effective with functions like document.getElementById which can be reduced to d[e]().

For a single callWith bracket notation, the relative cost*cost is 86 + name.length characters the first time. Each subsequent callaccess has a relative cost of 23 characters.

For standard invocationdot notation, all callsaccesses cost name.length + 1 (+1 for the .) characters.

Use this method if 86 + name.length + (23 * invocations(accesses - 1)) < invocationsaccesses * (name.length + 1).

ilen = invocationslength of property name
leni = minimum function lengthaccesses to gettake advantage

ilen | leni  =============== 1 | |   2  | 12 3 | | 7  4  | 4 6 5  | 3 5 6  | 3 4 7 | 3 8+ | 2 

* relative cost doesn't include the calling object, perens, or parameters. Essentially: a.XXX() vs a[XXX]() The number of accesses can also span multiple objects. If you access .length 4 or more times on different arrays, you can use the same variable holding the string 'length'.

###Use Array-Access for repeat function calls

This is definitely a balancing act between variable/function name length and number of invocations. Instead of calling a.longFunctionName() twice, it's shorter to save the name and call the function via array-access:

f='longFunctionName' a[f](b) a[f](c) //34 

this is especially effective with functions like document.getElementById which can be reduced to d[e]().

For a single call, the relative cost* is 8 + name.length characters. Each subsequent call has a relative cost of 2 characters.

For standard invocation, all calls cost name.length characters.

Use this method if 8 + name.length + (2 * invocations) < invocations * name.length

i = invocations len = minimum function length to get advantage

i | len ======= 1 | ∞ 2 | 12 3 | 7 4 | 6 5 | 5 6 | 4 

* relative cost doesn't include the calling object, perens, or parameters. Essentially: a.XXX() vs a[XXX]()

###Use bracket notation for repeat property access

This is definitely a balancing act between property name length and number of accesses. Instead of calling a.longFunctionName() with dot notation twice, it's shorter to save the name and call the function via bracket notation:

a[f='longFunctionName'](b) a[f](c) //34 

this is especially effective with functions like document.getElementById which can be reduced to d[e].

With bracket notation, the cost is 6 + name.length characters the first time. Each subsequent access has a cost of 3 characters.

For dot notation, all accesses cost name.length + 1 (+1 for the .) characters.

Use this method if 6 + name.length + (3 * (accesses - 1)) < accesses * (name.length + 1).

len = length of property name
i = minimum accesses to take advantage

len | i  ======== 1 |   2  |  3 | 7  4  | 4 5  | 3 6  | 3 7 | 3 8+ | 2 

The number of accesses can also span multiple objects. If you access .length 4 or more times on different arrays, you can use the same variable holding the string 'length'.

replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

A couple examples I've writtenI've written:

A couple examples I've written:

A couple examples I've written:

Mod Removes Wiki by Doorknob
edited body
Source Link
zzzzBov
  • 3.3k
  • 2
  • 19
  • 19
Loading
Rollback to Revision 3
Source Link
zzzzBov
  • 3.3k
  • 2
  • 19
  • 19
Loading
"self" is not magical like "this"
Source Link
Casey Chu
  • 1.8k
  • 13
  • 10
Loading
added 1223 characters in body
Source Link
zzzzBov
  • 3.3k
  • 2
  • 19
  • 19
Loading
edited body
Source Link
zzzzBov
  • 3.3k
  • 2
  • 19
  • 19
Loading
Source Link
zzzzBov
  • 3.3k
  • 2
  • 19
  • 19
Loading