Lets say I have a really heavy piece of computation, it involves two nested for loops goings over an array of size 1000*1000
The calculation takes about 1 minute to do.
I have a bunch of other stuff going on in the webpage especially user interaction. ( on-click, on-scroll, animation, etc )
Now I need to do the 60s calculation at some point. I can for example do:
setTimeout TwoForLoops,0this will defer the function call to until all the other on-click,on-scroll,etc events are cleared. however as the calculation takes 1 Minute, the user might trigger some event during that time. do you think if I just do the following I will prevent JavaScript from hanging. Assuming that heavyComputation takes about 0.06 millisecond to computate.
# _.map is from underscore@ library, basically does a for loop # HeavyComputation is a slow function with lots of operations made for my app NonBlockMap = (x,f)-> setTimeout (->_.map x,f),0 NonBlockHeavyComputation = (x) -> setTimeout (-> HeavyComputation(x)),0 TwoForLoops = (ArrayOfArray) -> SecondLoop = (element) -> NonBlockHeavyComputation element FirstLoop = (array) -> NonBlockMap array, SecondLoop return NonBlockMap ArrayOfArray,FirstLoopThis would essentially mean that the maximum javascript will hang for is about 0.06 millisecond but I am not so sure if this will work.
WhatDo ?
Deferring all heavy computation using setTimeout
366 Views Asked by sourcevault At
1
There are 1 best solutions below
Related Questions in JAVASCRIPT
- Angular Show All When No Filter Is Supplied
- Why does a function show up as not defined
- I count the time the user takes to solve my quiz using Javascript but I want the same time displayed on another page
- Set "More" "Less" font size
- Using pagination on a table in AngularJS
- How to sort these using Javascript or Jquery Most effectively
- how to fill out the table with next values in array with one button
- State with different subviews
- Ajax jQuery firing multiple time display event for the same result
- Getting and passing MVC Model data to AngularJS controller
- Disable variable in eval
- javascript nested loops waiting for user input
- .hover() seems to overwrite .click()
- How to sort a multi-dimensional array by the second array in descending order?
- How do I find the fonts that are not loading in a CORS situation ( MoovWeb )?
Related Questions in ASYNCHRONOUS
- Run a loop over a callback, node js
- run oncomplete event in async
- How are multiple requests to Task.Run handled from a resource management standpoint?
- Node JS Async Response
- ajax async: true statement execution order
- Need help making this translation function work with an array input
- How to check in an (Android) async task if the activity it was called from was finished?
- Async vs Horizontal scaling
- Task await fails
- Having two sequential steps running within a windows service
- Would async/await provide benefit over Task for intertwined statements?
- What is the best way to make two web pages communicate between each other back and forth?
- Get result from async closure - Unexpected found nil while unwrapping an Optional value
- Nested asynchronous calls using AngularJS
- Telerik Report Viewer don't work with jquery async: false
Related Questions in COFFEESCRIPT
- Reverse geocoding with a deferred object in a coffeescript class
- AngularJS - Losing controller scope within method
- Unpredictable dependency behaviour in Node JS while using the config npm module
- Call function from coffeescript file in html file
- Gulp fails and returns Unhandled 'error' event
- Object literal multiple assignment in CoffeeScript
- CasperJs Catch Timeout and Restart Process
- How to export a function from a module to another in coffeesricpt?
- CoffeeScript For NetBeans 8.0 Plugin: Set Target Folder For Compiles JS
- Jasmine spyOn with multiple returns
- Using after to clean up in mocha, problems with mongo
- Sort array of objects in javascript with user input
- Coffeescript input not disabled on page load
- Creating a gruntfile to compile Jekyll and push a subfolder to Github
- How to bind response of Ajax request to Model in EmberJs?
Related Questions in UNDERSCORE.JS
- Group and Sort object with Underscore.js
- How does the _() function (object wrapper) in Underscore.js work?
- Backbone.js Click event firing multiple times
- Underscore Convert array to object keys
- Underscore _.findWhere not finding element in array when it exists
- Adding the values from an object to an array of objects without overriding existing key values
- UnderscoreJS : filter part of string
- How to combine values in array by key?
- How to sort an array of objects by date range and creation date?
- Aggregating data on the client for use in a stacked bar chart
- Access JSON object key/value where value is an array using underscorejs
- How to partially substitute an Underscore.js template, or how to create a template from a template
- Does _.pluck preserves the original index of the "plucked" array?
- "document.write can be a form of eval"?
- Underscore.js _.map function : skip a value
Related Questions in NONBLOCKING
- A query on asynchronous response for Servlet request
- ZeroMQ pattern for load balancing work across workers based on idleness
- Tornado '@run_on_executor' is blocking
- C++ boost threads are locking GUI thread in MFC
- Ruby Net::Http split write and read operation for non-blocking
- Java / Scala Future driven by a callback
- How to make non-blocking OpenSSL connection?
- Gearman client NON_BLOCKING mode vs doBackground
- Can MS Access be Non-blocking, or is this just wishful thinking?
- How non-blocking API works?
- Timeout in select does not work after non blocking connect function call
- How does client handles deferred result in long running rest calls
- How to session_write_close() in Laravel?
- How to get large data via mongoose non-blocking?
- Sockets in C#, how can I asynchronously read and write data through a NetworkStream
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
I think your code queues all million HeavyComputation continuables up front, not as it progreses. The event loop will process each one in order before handling new user events, so you've still blocked for a minute.
You should use setImmediate though, or double-check that setTimeout of 0 does not sleep. Nodejs (what I work in; maybe chrome too?) always sleeps 1 ms.
Consider batching the heavy computation nested loop, something like: