I have some js code that does some computation (essentially, a bunch of for loops for matrix operations). My first attempt at a Ti app simply embedded a WebView with this JS code and let the WebView handle the calculation. This is reasonably fast and works fine.
I then decided I should probably be importing the js computation code directly into my Titanium app so that I can take advantage of JavaScriptCore on iOS. However, now the identical JS code runs out of memory after a few iterations through a for loop, and is also much slower. I am testing the code on the iOS simulator.
Before I spend too much time trying to resolve this, I am wondering if this is expected behavior -- i.e. if there is some speed advantage to running this kind of for-loop-heavy js code in a webview, and if Titanium code should really be used for interface stuff and other "native bridge" code?
I am very confused since the code is identical, so I don't have any ideas of where to start.
Sample function called from a webview or directly from Ti -- it's pretty simple... It's not anything really specific to this code though, rather it's just a bunch of work and array operations within a for loop:
function test(arr) { var r = 2; for (var y=0; y<arr.length; y++) { for (var x=0; x<arr[y].length; x++) { var sorted_p = []; for (var d=0; d<arr[y][x].length; d++) { sorted_p.push([]); } for (var h=-r; h<r; h++) { for (var w=-r; w<r; w++) { var cy = Math.min(arr.length-1, Math.max(0, y+h)); var cx = Math.min(arr[y].length-1, Math.max(0, x+w)); for (var d=0; d<arr[y][x].length; d++) { sorted_p[d].push(arr[cy][cx][d]); } } } } }This code results in the following error ONLY when called from Ti, not when run in a webview. The code also runs much slower:
myapp(96444,0xb0432000) malloc: *** mach_vm_map(size=8388608) failed (error code=3) [INFO] : *** error: can't allocate region [INFO] : *** set a breakpoint in malloc_error_break to debug
Application type: mobile Titanium Studio version: 3.2.0 Build: jenkins-titanium-rcp-master-76 (origin/master) Platform: iOS Simulator, iOS 7.0 Host OS: Mac OS X 10.9