Skip to content

Drastically faster templating approach #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

zspitzer
Copy link

@zspitzer zspitzer commented Sep 15, 2016

The current templating approach is really inefficient, as it recursively loops thru every
passed available variable for substituion and applies a regex for each one.

It's much faster to initially parse the template and then only process those strings which are
actually present in the template.

This revised implementation both caches the parsed templates and then only processes the strings which are present.

The performance improvement is quite drastic, especially with large tables. A large table was previously taking 15s to render in Chrome, now takes less than 1s with this approach

there is no need to clone() here,  it creates a memory leak as it's not attached the to DOM and therefore isn't removed by remove()
Trigger an event when columns are toggled on or off
The current templating approach is really inefficient, as it recursively loops thru every 
passed available variable for substituion and applies a regex for each one. 

It's much faster to initially parse the template and then only process those strings which are 
actually present in the template.

This revised implementation both caches the parsed templates and then only processes the string which are present.

The performance improvement is quite drastic, especially with large tables. A large table was previously taking 15s to render 
in Chrome, now takes less than 1s with this approach
@yooakim
Copy link

yooakim commented Dec 8, 2016

Any chanche this fill be merged with the main branch? Seems nice to speed up rendering of large tables!

@alvaropag
Copy link

Is this project still active? I'm using the library, it's awesome, but I don't see any new commits (but many new pull requests).

@rstaib
Copy link
Owner

rstaib commented Dec 8, 2016

I have recently verified the pull request, but it seems not to work correctly in any situation. The info footer Showing {{ctx.start}} to {{ctx.end}} of {{ctx.total}} entries for example was not rendered. Would be nice to see some tests for it.

@zspitzer
Copy link
Author

zspitzer commented Dec 9, 2016

the problem is a nested template string
infos: "Showing {{ctx.start}} to {{ctx.end}} of {{ctx.total}} entries",
https://github.com/zspitzer/jquery-bootgrid/blob/master/src/public.js#L337

rather than add yet another level of recursion which would be repeated for every templated string, what do you think about refactoring this template string into the common templates definition?
https://github.com/zspitzer/jquery-bootgrid/blob/master/src/public.js#L397

rather than needing to recursively process every string, move the templated string for infos into the template definitions
@zspitzer
Copy link
Author

zspitzer commented Dec 9, 2016

with the small dataset in the demo, it's taking 36ms to render with the old approach and 20ms to render using this new approach

added buttons to append 10, 100 and 1000 rows with logging of rendering time (on screen and to console)
@zspitzer
Copy link
Author

zspitzer commented Dec 10, 2016

I have updated the demo with more append options (10 ,100 and 1000 rows) and logging render time to both the browser console and on screen

using Chrome 55, win10 x64
old approach
[appendData: 10 rows] : 52.189ms
[appendData: 100 rows] : 526.082ms
[appendData: 1000 rows] : 4707.418ms

new approach
[appendData: 10 rows] : 14.790ms
[appendData: 100 rows] : 29.907ms
[appendData: 1000 rows] : 278.368ms

just testing the new approach, appending extra context

IE 11
[appendData: 100 rows] : 180.595ms
[appendData: 1000 rows] : 5,827.922ms
[appendData: 1000 rows] : 19,008.279ms
EDGE
[appendData: 100 rows] : 84.63ms
[appendData: 1000 rows] : 1,270.115ms
[appendData: 1000 rows] : 4,146.51ms
Firefox
[appendData: 100 rows] : 41.05ms
[appendData: 1000 rows] : 168.66ms
[appendData: 1000 rows] : 325.76ms
Chrome
[appendData: 100 rows] : 29.359ms
[appendData: 1000 rows] : 273.077ms
[appendData: 1000 rows] : 664.405ms

don't set the replacement value to null when a property doesn't exist
added back in the old method and evaluate the template string using both approaches,
log out to console any different results found
@zspitzer
Copy link
Author

I have added a test which just compares the output from both the new and old approaches. I found a problem will null being returned when a property didn't exist.

The old approach left the missing properties in as the template string, see the {{ctx.style}} below from the demo page

<td class="select-cell" style="{{ctx.style}}"><input name="select" type="checkbox" class="select-box" value="9" /></td>

refactored functions out from string and array protoypes
included appendRow changes from rstaib#285 (50% faster in Chrome, 10% Edge,
15% FF)
remove prototypes from string and array
merge in appendRow rstaib#285  improves Chrome 50%, FF 15%, Edge 10%
@zspitzer
Copy link
Author

Refactoring the functions out of the String prototypes into functions saved 400ms in IE and Edge
https://developer.mozilla.org/en-US/docs/Web/JavaScript/The_performance_hazards_of__%5B%5BPrototype%5D%5D_mutation

Refactoring out Array didn't make so much of a difference tho, but it's best practise #148

Including #285 speed up Chrome by 50%, Firefox by 15% and Edge by 10%, IE still sucks

no longer needed since appendRows
@zspitzer
Copy link
Author

The remaining performance problems in Edge are simply browser problem
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10152783/

@eazuka
Copy link

eazuka commented Jul 27, 2017

What is the problem stopping this from being merged for this long?

@jamespsterling
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants