Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
feat(service): improve messageformat.js output caching
Browse files Browse the repository at this point in the history
Previously, the $translateMessageFormatInterpolation interpolator only cached results when both the
ICU MessageFormat string and the interpolation parameters matched. With this change, the compiled
function output by messageformat.js is cached, resulting in fewer cache misses that would require
the comparatively heavier $mf.compile() operation to be run.
  • Loading branch information
eemeli authored and knalli committed Mar 20, 2016
1 parent 3366c11 commit cb31608
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/service/messageformat-interpolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ function $translateMessageFormatInterpolation($translateSanitization, $cacheFact
interpolationParams = interpolationParams || {};
interpolationParams = $translateSanitization.sanitize(interpolationParams, 'params');

var interpolatedText = $cache.get(string + angular.toJson(interpolationParams));
var compiledFunction = $cache.get('mf:' + string);

// if given string wasn't interpolated yet, we do so now and never have to do it again
if (!interpolatedText) {
// if given string wasn't compiled yet, we do so now and never have to do it again
if (!compiledFunction) {

// Ensure explicit type if possible
// MessageFormat checks the actual type (i.e. for amount based conditions)
Expand All @@ -117,13 +117,12 @@ function $translateMessageFormatInterpolation($translateSanitization, $cacheFact
}
}

interpolatedText = $mf.compile(string)(interpolationParams);
interpolatedText = $translateSanitization.sanitize(interpolatedText, 'text');

$cache.put(string + angular.toJson(interpolationParams), interpolatedText);
compiledFunction = $mf.compile(string);
$cache.put('mf:' + string, compiledFunction);
}

return interpolatedText;
var interpolatedText = compiledFunction(interpolationParams);
return $translateSanitization.sanitize(interpolatedText, 'text');
};

return $translateInterpolator;
Expand Down

0 comments on commit cb31608

Please sign in to comment.