Prevent browser freezing on runaway code

143 views
Skip to first unread message

Craig in Calgary

unread,
Apr 24, 2012, 11:43:44 AM4/24/12
to tiddl...@googlegroups.com
When I experiment with code that loops through tiddlers, tags, etc., occasionally I (temporarily) create a monster: the browser freezes because javascript is running and not likely to finish until the computer runs out of memory. I would like to implement a wrapper I can place around test code that can be configured to halt the code after a pre-configured amount of time or if a pre-configured amount of memory is allocated. Halt after X ms would be a sufficient solution. Halt if memory allocation > X MB would be gravy.

1. Can such a safety net be coded and successfully implemented?
2. Who among you TW gurus would create such a masterpiece, knowing that while you will have my undying praise, the only other reward for your efforts will be the satisfaction of knowing you have made a valuable contribution to our community.

Thank you to anyone providing suggestions, feedback, and/or solutions.

Begging Craig in Calgary

PMario

unread,
Apr 25, 2012, 11:37:01 AM4/25/12
to TiddlyWiki
Hi Craig,
This can happen ;)

a)
Most modern browsers have a built in "feature" that warns you if a js
programm goes wild [1]. eg: FF automatically stopps "long running
scripts" with a message. May be you disabled this behaviour at your
browser? If so, you should enable it again.

b)
Some time ago, I did find some nice articles, about optimizing loop
handling in the browser [2]. Especially the blog posts "part 1"[3] and
"part 4"[4] may be interesting for you.

====
c)
I'd try this mechanism first.

With TW, the DOM handling (as described in part 4) can be a reason,
why your loops are "slow". If something changes the TW layout. Eg: if
you programmatically create a new tiddler. The TW core needs to update
the right sidebar ..., which is "slow", if it needs to be done very
often. In TW there are 3 functions to temporary stop this browser
layout redraw.

store.suspendNotifications();
// your loop goes here!
store.resumeNotifications();
store.notifyAll();

see tiddlywiki.org [5] for more info
====

d)
Using the search term >> non blocking loop javascript << with
google, It did point me to a little library called "Clumpy" [6]
I had a short look, and it is the wrapper function you requested :)
You should start with the "for_loop" [7].

The library contains functions to create user feedback. eg: 10% -
20% .... 100% done!
Stopping, starting a loop is possible too. So this would be worth a
try.

====

c) and d) may also work, with fET. Note: may work.

hope this helps
-mario

[1] http://www.nczonline.net/blog/2009/01/05/what-determines-that-a-script-is-long-running/
[2] http://www.nczonline.net/blog/2010/02/09/announcing-high-performance-javascript/
[3] "part 1" http://www.nczonline.net/blog/2009/01/13/speed-up-your-javascript-part-1/
[4] "part 4" http://www.nczonline.net/blog/2009/02/03/speed-up-your-javascript-part-4/
[5]
http://tiddlywiki.org/#TiddlyWiki.prototype.notifyAll%20TiddlyWiki.prototype.suspendNotifications%20TiddlyWiki.prototype.resumeNotifications
[6] http://www.tumuski.com/code/clumpy/overview/
[7] http://www.tumuski.com/code/clumpy/reference/#for_loop

Craig in Calgary

unread,
May 5, 2012, 9:49:20 AM5/5/12
to tiddl...@googlegroups.com
Mario, you have given me much to evaluate. Thank you.
Reply all
Reply to author
Forward
0 new messages