JavaScript Functions for Speed and Profit
The movie below is from one of our most popular demos from “Year in Review” at DevCon 2014. I was talking about what a breakthrough custom functions were back in 2004 and how they unlocked a whole new kind of code-sharing among FileMaker developers. JavaScript functions in FileMaker may unlock even more potential.
Jason Young introduced the idea of using JavaScript as a calculator–as a library of functions–instead of using it for perhaps the more obvious purpose of building interface elements. (Jason’s original article on this can be seen as part 1 of this post.) This gives us a huge opportunity to share and borrow code from the immense catalogue of JavaScript functions and examples.
As just one example, we (Jason) rewrote the venerable “namwoB” function in JavaScript. namwoB was the first recursive functions I ever saw and was posted by Andy LeCates back in March of 2004. The title comes from Court Bowman’s name spelled backwards… which is all the functions does, rewriting a string of text in reverse. This simplicity makes it a great way to talk about the different kinds of recursion in FileMaker and what a faster version of the function might look like in JavaScript.
Our example file shows the same recursive custom functions done as a “stack” recursive function (limited to about 10k loops), as a “tail” recursive function (limited to 49,999 loops), and as a JavaScript function executed in a webviewer and then passed into a FileMaker script.
JavaScript Functions in FileMaker are Fast
Looping through 49,000 characters took our tail-recursive custom function almost 3 seconds.
The JavaScript version of the function looped through 10 times that much text–nearly 500,000 characters–in 3/10ths of a second.
And thanks to the improved fmp url in FileMaker 13, we can grab the results of functions like these quite easily. The movie below demos this and walks through our example file. It also gives a nice side-effect-demo of how webviewers get their own thread in FileMaker Pro and what that may mean for trying to measure performance.
11 Comments
Hi I am trying to learn this technique but some how it is not working for me. I have tried sample file in FileMaker 13 and it crashes. In FileMaker 14 and 15 my Mac swaps memory as crazy and the system becomes unresponsive. I am using El Capitan. Could it be that Webkit is leaking memory? Any help will be appreciated. Thank you
Hi David, It requires 13.0v3 or higher so that may be the issue in 13. You can also probably get past the speed in 14 and 15 by using < 49K characters in the JS example. Start with a few hundred characters and you can still see how it works. (On my FM15 machine it hung the first time I opened it and tried 49K characters, after closing and re-opening FM15 it seems fine and does 49,999 characters in 0.39 seconds.)
This is excellent. Maybe this will encourage Filemaker Inc. to include Javascript natively in Filemaker in some future version.
+1!!
Oh, yeah…a native ExecuteJS() function gets my vote!!
Thanks for posting this demo and making a video, John (and Jason). I appreciate how much time it takes to develop new techniques like this and then find the time to properly share them on the web. I’m sure the rest of the FM community likes it, too!
Great stuff, as ever, John! It’s really wonderful to see the FileMaker community getting excited about the huge potential of JavaScript.
On our end, we’ve been busy cooking up some new techniques we’ll be sharing soon as well. Looking forward to trading our best ideas and exploring new uses of JavaScript together as a community!
Luv your stuff, Brian!
Thanks John. This is very cool and it makes me wonder about using Javascript to parse FMP fields using regular expressions, which is not easily done natively in FMP.
Nice idea. I believe the example file beezwax posted here has a regex example: http://buzz.beezwax.net/2012/06/12/go-javascript-go
Thanks John. I forgot about Brian Schick’s article.