Solved

Knockout & jQuery takes a VERY long time using push...

Posted on 2014-11-28
6
218 Views
Last Modified: 2014-12-02
Hi all,

I am using jQuery and Knockout on my webapp, but when I try to process an array (~400 elements) the request can take a very long time to execute (around 60s!)

JS
AppViewModel.page_model = {
    my_log_entries : ko.observableArray(),
    };

function AjaxReportsMyLog()
{
	var myCommand = [];
	myCommand.cmd = "GetReportMyLog";
	var params = JSON.stringify({LoginName:AppViewModel.user_session.UserDetails.LoginName, SessionID:AppViewModel.user_session.UserDetails.SessionID });
	myCommand.data = params;
	myCommand.onSuccess = function(o)
	{
		AppViewModel.page_model.last_updated(new Date().toLocaleString());
		if(JSON.stringify(AppViewModel.page_model.my_log_entries()) != JSON.stringify(o.ajaxdata.ListOfEntries))
		{
			alert('Starts here...');
			$.each(o.ajaxdata.ListOfEntries, function(key, val)
			{
				AppViewModel.page_model.my_log_entries.push(val);
			});
			alert('60s later, gets here!!');
		}
	}
	myCommand.onError = function(o)
	{
		Utils.Dialog_Server_Error(o.errorThrown);
	}
	Api.Exe(myCommand);
};	

$(document).ready(function()
{
	. . 
	. . 
    AppViewModel.page_model.my_log_entries.removeAll();
	AjaxReportsMyLog();
});

Open in new window


html
<script type="text/html" id="my-log-template">
	<table class="report">
		<tr>
			<th class="mv-log2">Field 1</th>
			<th class="mv-log4">Field 2</th>
			<th class="my-log3">Field 3</th>
		</tr>
		<tbody id="my-log-records" data-bind="foreach: $data">
			<tr class="section">
				<td data-bind="text: $data.Field1"></td>
				<td data-bind="text: $data.Field2"></td>
				<td data-bind="text: $data.Field3"></td>
			</tr>
		</tbody>
	</table>
</script>

Open in new window


It all seems to work just fine, just takes a very long time!!!

Any help would be appreciated
0
Comment
Question by:jatkin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 43

Expert Comment

by:Rob
ID: 40470873
Might be a silly question but you have called the applyBindings method?
i.e. "ko.applyBindings(AppViewModel.page_model);" ?
0
 
LVL 43

Expert Comment

by:Rob
ID: 40470876
Also you've encapsulated your table in a <script> tag? why?  that's not necessary.
0
 
LVL 43

Expert Comment

by:Rob
ID: 40470897
I've set up a demo here: http://jsbin.com/remeyi/1/edit?html,js,output

I've had to change some code and add a call to some dummy data (over 400 records: http://jsbin.com/futoze/1.json) .  This runs in about a couple of seconds.  Is there anything more complicated to your actual data than what you see here?
0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 4

Author Comment

by:jatkin
ID: 40473442
Hi,
Thanks for the suggestions...
I had applied the bindings.
The SCRIPT tag was inherited from the previous developer :-) It appears that as this form was previously used for multiple reports, they used the ID of the SCRIPT to run the appropriate section. Not needed anymore now so I'll remove it.
I do believe however that I have identified the issue... well, I have fixed the delay anyway...
As the "AppViewModel.page_model.my_log_entries" is an observable object, each time an entry is added to it, it is reevaluated. Looks like this takes some time in an array this time (quite why it is 60s I don't know!)
However, I have applied the 'pausable' extension to knockout:
https://github.com/Areson/Knockout-Extensions/wiki/Knockout.Pausable
and it seems to be resolved. Now takes a fraction of a second to process:

AppViewModel.page_model.my_log_entries.pause();
$.each(o.ajaxdata.ListOfEntries, function (key, val)
{
    AppViewModel.page_model.my_log_entries.push(val);
});
AppViewModel.page_model.my_log_entries.resume();

Open in new window

0
 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 40474356
Yes, that does make sense.  Great to hear it's working for you.  

Going forward, If you don't want to use a plugin, you could just write the values to a temporary array, which is what I do anyway.

e.g.
var temp = []; // empty array
$.each(o.ajaxdata.ListOfEntries, function (key, val)
{
    temp.push(val);
});

AppViewModel.page_model.my_log_entries(temp);

Open in new window


Now the my_log_entries observable is only being updated once
0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 40475461
This looks like a much neater way that pausing the observable!
Thanks a lot for the help.
James
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question