Skip to content

Commit

Permalink
Doubled Polymer.CaseMap.dashToCamelCase performance with simplified…
Browse files Browse the repository at this point in the history
… and once compiled RegExp.

5 times faster `Polymer.CaseMap.camelToDashCase` using simplified replace part, simplified and once compiled RegExp.
  • Loading branch information
nazar-pc committed Jan 7, 2016
1 parent d7567b7 commit 90938e3
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions src/lib/case-map.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,24 @@
Polymer.CaseMap = {

_caseMap: {},
_rx: {
dashToCamel: /-[a-z]/g,
camelToDash: /([A-Z])/g
},

dashToCamelCase: function(dash) {
var mapped = Polymer.CaseMap._caseMap[dash];
if (mapped) {
return mapped;
}
// TODO(sjmiles): is rejection test actually helping perf?
if (dash.indexOf('-') < 0) {
return Polymer.CaseMap._caseMap[dash] = dash;
}
return Polymer.CaseMap._caseMap[dash] = dash.replace(/-([a-z])/g,
function(m) {
return m[1].toUpperCase();
}
return this._caseMap[dash] || (
this._caseMap[dash] = dash.indexOf('-') < 0 ? dash : dash.replace(this._rx.dashToCamel,
function(m) {
return m[1].toUpperCase();
}
)
);
},

camelToDashCase: function(camel) {
var mapped = Polymer.CaseMap._caseMap[camel];
if (mapped) {
return mapped;
}
return Polymer.CaseMap._caseMap[camel] = camel.replace(/([a-z][A-Z])/g,
function (g) {
return g[0] + '-' + g[1].toLowerCase()
}
return this._caseMap[camel] || (
this._caseMap[camel] = camel.replace(this._rx.camelToDash, '-$1').toLowerCase()
);
}

Expand Down

0 comments on commit 90938e3

Please sign in to comment.