Skip to content

Instantly share code, notes, and snippets.

@tivac
Created September 20, 2012 19:27
Show Gist options
  • Save tivac/3757849 to your computer and use it in GitHub Desktop.
Save tivac/3757849 to your computer and use it in GitHub Desktop.

Wondered what would happen if we tried to be more efficient about variable referencing. Turns out to not be a size savings after gzip, but might be infinitesimally faster to execute due to removing two property lookups.

Original:

document.documentElement.className = document.documentElement.className.replace(/(\s|^)no-js(\s|$)/, '$1' + 'js' + '$2');

121, gzips to 96 (20% smaller)

Compressed:

document.documentElement.className=document.documentElement.className.replace(/(\s|^)no-js(\s|$)/,"$1js$2")

107, gzips to 85 (20% smaller)

Rewritten:

(function(el) {
    el.className = el.className.replace(/(\s|^)no-js(\s|$)/, '$1js$2');
}(document.documentElement));

119, gzips to 115 (3% smaller)

Compressed:

(function(e){e.className=e.className.replace(/(\s|^)no-js(\s|$)/,"$1js$2")})(document.documentElement)

102, gzips to 102 (0% change)

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