I have a Jobs table view that displays all the jobs of a user. The Jobs collection fetch() can potentially return contain thousands of records. I ran a test and inserted 1000 Job records in the DB and performed a fetch() on the collection. However, 1000 records seem too much for the browser to handle since inserting 1000 DOM table rows seem to cause the browser to freeze.
Is there a better way to optimize the rendering of the rows so it performs faster? I know you could always do a partial fetch (fetch initialy 100 records and additionally fetch 100 records every time the user scrolls to the bottom of the screen), but I'm generally against this idea since scrolling down 100 records and having to wait 3-4 seconds before it renders an additional 100 records seem to result in poor user experience.
Here's my code:
FM.Views.JobTable = Backbone.View.extend({
initialize: function(){
_.bindAll(this, 'render', 'refresh', 'appendItem');
this.collection.bind('add', this.appendItem, this);
this.collection.bind('reset', this.refresh, this);
},
render: function(){
this.el = ich.JobTable({});
$(this.el).addClass('loading');
return this;
},
refresh: function(){
$('tbody tr', this.el).remove();
$(this.el).removeClass('loading');
_(this.collection.models).each(function(item){ // in case collection is not empty
this.appendItem(item);
}, this);
return this;
},
appendItem: function(item){
var jobRow = new FM.Views.JobTableRow({
model: item
});
$('tbody', this.el).prepend(jobRow.render().el);
$(jobRow).bind('FM_JobSelected', this.triggerSelected);
}
});
FM.Views.JobTableRow = Backbone.View.extend({
tagName: 'tr',
initialize: function(){
_.bindAll(this, 'render', 'remove', 'triggerSelected');
this.model.bind('remove', this.remove);
},
render: function(){
var j = this.model.toJSON();
j.quantity = j.quantity ? number_format(j.quantity, 0) : '';
j.date_start = date('M j Y', j.date_start);
j.date_due = j.date_due ? date('M j Y', strtotime(j.date_due)) : '';
j.paid_class = j.paid;
j.status_class = j.status;
j.paid = slug2words(j.paid);
j.status = slug2words(j.status);
this.el = ich.JobTableRow(j);
$(this.el).bind('click', this.triggerSelected);
return this;
}
});
I have a Jobs table view that displays all the jobs of a user. The Jobs collection fetch() can potentially return contain thousands of records. I ran a test and inserted 1000 Job records in the DB and performed a fetch() on the collection. However, 1000 records seem too much for the browser to handle since inserting 1000 DOM table rows seem to cause the browser to freeze.
Is there a better way to optimize the rendering of the rows so it performs faster? I know you could always do a partial fetch (fetch initialy 100 records and additionally fetch 100 records every time the user scrolls to the bottom of the screen), but I'm generally against this idea since scrolling down 100 records and having to wait 3-4 seconds before it renders an additional 100 records seem to result in poor user experience.
Here's my code:
FM.Views.JobTable = Backbone.View.extend({
initialize: function(){
_.bindAll(this, 'render', 'refresh', 'appendItem');
this.collection.bind('add', this.appendItem, this);
this.collection.bind('reset', this.refresh, this);
},
render: function(){
this.el = ich.JobTable({});
$(this.el).addClass('loading');
return this;
},
refresh: function(){
$('tbody tr', this.el).remove();
$(this.el).removeClass('loading');
_(this.collection.models).each(function(item){ // in case collection is not empty
this.appendItem(item);
}, this);
return this;
},
appendItem: function(item){
var jobRow = new FM.Views.JobTableRow({
model: item
});
$('tbody', this.el).prepend(jobRow.render().el);
$(jobRow).bind('FM_JobSelected', this.triggerSelected);
}
});
FM.Views.JobTableRow = Backbone.View.extend({
tagName: 'tr',
initialize: function(){
_.bindAll(this, 'render', 'remove', 'triggerSelected');
this.model.bind('remove', this.remove);
},
render: function(){
var j = this.model.toJSON();
j.quantity = j.quantity ? number_format(j.quantity, 0) : '';
j.date_start = date('M j Y', j.date_start);
j.date_due = j.date_due ? date('M j Y', strtotime(j.date_due)) : '';
j.paid_class = j.paid;
j.status_class = j.status;
j.paid = slug2words(j.paid);
j.status = slug2words(j.status);
this.el = ich.JobTableRow(j);
$(this.el).bind('click', this.triggerSelected);
return this;
}
});
Share
Improve this question
asked Jan 21, 2012 at 14:12
user2385136user2385136
2 Answers
Reset to default 5it all depends on what experience the user needs, what will he have to do with the jobs, your user, is it a potential job candidate that is looking for a certain job? or is it sort of an administration app where the user is someone managing jobs?
in general putting 1000 items on 1 page is bad userexperience, working with loading additional jobs on scroll down is kind of a hot feature these days like facebook, twitter... this might be good for ments but jobs is something else, one needs a way to jump from beginning to end without having to click "more" 10 times, or scroll down 10 times.
possible solutions:
- so, paging is of course another option, using a pager is defenately a way to work with too many items, and giving the person a way to jump from page 1 to 10 without passing trough the 9 others.
- another thing you can do is build in filters, one can search jobs by: location, firm, sector, ... this will reduce the size of the collection that is visible at all times.
pure technical solutions:
you should read up on this blog post, it starts about how to get the exact clicked item from a view if you rendered all items of a collection in just 1 view, but evolves into the issue at hand here, having 1000 separate view's 1 per job being added to the jobListView, or having those jobs added within the jobListView so there is only 1 view.
The latter could if implemented correctly, greatly reduce the interaction of your app with the DOM. With implemented correctly i do mean, adding all jobs in a for loop, by adding them to a table / list in memory, and only at the very end, attach the table / list into the DOM, this reduces your code to 1 DOM interaction, rather than 1000 appends.
Yes, Derick does lean more towards rendering 1 view per model, though he does not touch the subject of performance except for a small: first make it then make it fast statement which does not provide any solutions for you. And if your list of jobs is just a listing and a link through to a job detail page, without many events, the 1 view to rule them all option is still very valid.
Your poor performance is probably because you're adding rows directly to the DOM. Each time you do this the browser will reflow.
I would build the table in memory and then add the whole table to the DOM once its constructed.