views:

59

answers:

1

Hey guys,

So I'm sure this is probably at least mostly insane, but I was just thinking about AS3/JS interaction and it got me wondering - does anyone know exactly how inefficient calling JS via AS3 is? For example, if you do the following:

import flash.external.ExternalInterface;
ExternalInterface.call("(function() { /* here's a block of code */ })");

Do these calls need to be eval()ed in the end, or are they able to be passed natively?

Regardless: let's be hypothetical for a second and say that you were a heretic and actually stored a good deal of Javascript within a utility SWF (1x1, offscreen, whatever), and had basic DOM events bound to hand those events to AS3 when they fire, which thus uses ExternalInterface to execute the JS immediately - thus the only part of your Javascript that is ever loaded is a little bit to communicate with Flash. A naive person might even say "you could condense numerous JS files into one loaded SWF file, separating them into different MC's or whatever!", but really, that's not the point, and that won't help us any after the page is already loaded.

I've used AS3 and its Socket class in lieu of XHR polling (etc), so I haven't been disappointed with AS3/JS interaction so far. AFAIK AS3 is executed inside the Flash VM which means that it is automatically less efficient than Javascript, correct? Can someone shed some light on how terrible this would be, efficiency-wise?

+1  A: 

It is not impossible to deconstruct the AS3 code within a Flash movie. While it would thwart the casual observer, if someone with skills wanted to get at your scripts they would be able to. You would only be throwing a roadblock in their path, not an impassable one, and possibly one that is even less difficult to crack than the code produced by standard JS obfuscators.

As for performance, Flash execution compares well with browser-hosted Javascript interpreters. See http://jacksondunstan.com/articles/232 for one set of comparisons. It is close to the fastest JS for sheer code execution*.

N.B. — And for graphical UI tasks nothing else (i.e., HTML 5) so far has even come close. This may change with IE9's new JS engine and its hardware acceleration, but it is not clear whether Adobe will be able to tap into the same thing at some point.

Robusto
Ah yes, definitely... I suppose "obfuscation" is a bit misleading, though the point would be to obscure it as much as possible; having obfuscated JS within AS3 would mean that not only would the SWF have to be decompiled, but the code within would need to be heavily rewritten/reformatted to make much sense anyway. Absolutely not meant to be a perfect solution; more of a POC.
mway
Can you clarify your NB? This wouldn't have anything to with Flash UI; only for passing/executing Javascript. A UI could be part of that, sure, but only insofar as it is constructed within the JS that Flash is giving to the browser to execute.
mway
That's why I added it as a postscript. It's not relevant from a strict code-execution standpoint, but may be of interest depending on other factors.
Robusto