Skip to content

Commit 787a82a

Browse files
committed
MAGE-1000 Compare alternate rendering via Mustache
1 parent 6ca72f1 commit 787a82a

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

view/frontend/requirejs-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var config = {
2828
'recommendJs' : 'Algolia_AlgoliaSearch/js/internals/recommend-js.min',
2929
'rangeSlider' : 'Algolia_AlgoliaSearch/js/navigation/range-slider-widget',
3030
// Legacy
31-
'algoliaHoganLib' : 'Algolia_AlgoliaSearch/js/lib/hogan.min'
31+
'algoliaHoganLib' : 'Algolia_AlgoliaSearch/js/lib/hogan.min',
32+
'algoliaMustacheLib' : 'Algolia_AlgoliaSearch/js/lib/mustache.min'
3233
},
3334
deps : [
3435
'algoliaInstantSearch',

view/frontend/web/js/instantsearch.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ define([
22
'jquery',
33
'algoliaBundle',
44
'algoliaHoganLib',
5+
'algoliaMustacheLib',
56
'Magento_Catalog/js/price-utils',
67
'algoliaCommon',
78
'algoliaInsights',
89
'algoliaHooks',
9-
], function ($, algoliaBundle, Hogan, priceUtils) {
10+
], function ($, algoliaBundle, Hogan, Mustache, priceUtils) {
1011
$(function ($) {
1112
/** We have nothing to do here if instantsearch is not enabled **/
1213
if (
@@ -83,10 +84,10 @@ define([
8384
*
8485
* For templating is used Hogan library
8586
* Docs: http://twitter.github.io/hogan.js/
87+
*
88+
* Alternatively use Mustache
89+
* https://github.com/janl/mustache.js
8690
**/
87-
var wrapperTemplate = Hogan.compile(
88-
$('#instant_wrapper_template').html()
89-
);
9091
var instant_selector = '#instant-search-bar';
9192

9293
var div = document.createElement('div');
@@ -100,14 +101,28 @@ define([
100101
$('.algolia-instant-results-wrapper').append(
101102
'<div class="algolia-instant-selector-results"></div>'
102103
);
104+
105+
const template = $('#instant_wrapper_template').html();
106+
const templateVars = {
107+
second_bar : algoliaConfig.instant.enabled,
108+
findAutocomplete: findAutocomplete,
109+
config : algoliaConfig.instant,
110+
translations : algoliaConfig.translations,
111+
};
112+
const hoganStart = performance.now();
113+
const wrapperTemplate = Hogan.compile(template);
114+
const hoganResult = wrapperTemplate.render(templateVars);
115+
const hoganEnd = performance.now();
116+
console.log("Hogan execution time: %s ms", hoganEnd - hoganStart);
117+
118+
const mustacheStart = performance.now();
119+
const mustacheResult = Mustache.render(template, templateVars);
120+
const mustacheEnd = performance.now();
121+
console.log("Mustache execution time: %s ms", mustacheEnd - mustacheStart);
122+
103123
$('.algolia-instant-selector-results')
104124
.html(
105-
wrapperTemplate.render({
106-
second_bar : algoliaConfig.instant.enabled,
107-
findAutocomplete: findAutocomplete,
108-
config : algoliaConfig.instant,
109-
translations : algoliaConfig.translations,
110-
})
125+
mustacheResult
111126
)
112127
.show();
113128

view/frontend/web/js/lib/mustache.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)