Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Running JavaScript Efficiently in a Java World

7,585 views

Published on

Describes how we bridged JavaScript (V8) and Java using J2V8.

Published in: Software
  • Have you ever heard of taking paid surveys on the internet before? We have one right now that pays $50, and takes less than 10 minutes! If you want to take it, here is your personal link ●●● http://ishbv.com/surveys6/pdf
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • ★★ How Long Does She Want You to Last? ★★ A recent study proved that the average man lasts just 2-5 minutes in bed (during intercourse). The study also showed that many women need at least 7-10 minutes of intercourse to reach "The Big O" - and, worse still... 30% of women never get there during intercourse. Clearly, most men are NOT fulfilling there women's needs in bed. Now, as I've said many times - how long you can last is no guarantee of being a GREAT LOVER. But, not being able to last 20, 30 minutes or more, is definitely a sign that you're not going to "set your woman's world on fire" between the sheets. Question is: "What can you do to last longer?" Well, one of the best recommendations I can give you today is to read THIS report. In it, you'll discover a detailed guide to an Ancient Taoist Thrusting Technique that can help any man to last much longer in bed. I can vouch 100% for the technique because my husband has been using it for years :) Here's the link to the report ➤➤ https://tinyurl.com/rockhardxx
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Your opinions matter! get paid for them! click here for more info...◆◆◆ https://tinyurl.com/make2793amonth
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Running JavaScript Efficiently in a Java World

  1. 1. J2V8 Running JavaScript Efficiently in a Java World R. Ian Bull EclipseSource @irbull
  2. 2. Are You a JavaScript Developer? • This talk is not for you. But you’ll want this information! • Running JavaScript in Java environments • Optimizing JavaScript for Java deployment • Java concepts bleeding into JavaScript • Can we unleash performant JavaScript in a Java System?
  3. 3. Example — Tabris.js • Mobile framework • Apps written in JavaScript • Native iOS and Android Apps • Bindings to native UI components • JavaScript was fast, Java was slow
  4. 4. Tabris.js • Tight integration between the mobile platform and JS
  5. 5. Android and JS • Android bindings written in Java • No decent Java / JavaScript integration on Android • Nashorn is not available • Rhino slooooow
  6. 6. What About V8? • V8 is a highly performant Javascript runtime • Written entirely in C++ • Workhorse behind node.js & Chrome http://ariya.ofilabs.com/2014/03/nashorn-the-new-rhino-on-the-block.html
  7. 7. J2V8 • Can we leverage V8 for Android development? • Create a thin JNI layer above V8 • Expose V8 API in Java • Open Source
  8. 8. J2V8 Design • C++ (JNI) Bindings between Java & JavaScript • Two GCs and unmanaged memory model in the middle V8 J2V8 — C++ Java I Live Here
  9. 9. J2V8 Design • Make JavaScript shine in an enterprise Java World • Standard Java APIs • Efficient Java / JavaScript bindings • No Java system knowledge in JavaScript • Multithreaded Java & JavaScript integration
  10. 10. Script Execution Example (Java) public static void main(String[] args) {
 V8 v8 = V8.createV8Runtime(); Object result = v8.executeScript("1+1"); System.out.println("Result: " + result); v8.release();
 }
  11. 11. Script Execution Example (JavaScript)
  12. 12. JavaScript Objects • Access JavaScript objects from Java • No special JS required • Object are lazily copied to Java v8.executeScript("person = {name : {first : 'john', last:'smith'} };"); String name = v8.get(“person”).get(“name”).get(“first”);
  13. 13. Lists and Maps Utilities • Working with V8Objects is highly performant but complex • Access JSObjects using java.util • JavaScript Object mapped to java.util.Map • JavaScript Array mapped to java.util.List • In short: It works with common Java API
  14. 14. Calling JS Functions • JavaScript Functions can be called from Java • Use the results directly in Java • Java Methods can be called from JavaScript • Pass JavaScript Objects to Java
  15. 15. Calling JavaScript • Any Java Method can be registered as a Function • Call the Function as you would any other Function • Java Methods can be specified with ‘var args’ JavaCallback callback = new MyJavaCallback(); v8.registerJavaMethod(callback, “foo"); v8.executeScript(“foo(‘a’, false, undefined)”);
  16. 16. JavaScript Exceptions • JavaScript errors / exceptions propagate to Java
  17. 17. Typed Arrays V8Array result = (V8Array) v8.executeScript("" + "var buf = new ArrayBuffer(100);" + "var ints = new Int32Array(buf); " + "for(var i = 0; i < 25; i++) {" + " ints[i] = i;" + "}; " + “ints"); int[] ints = result.getIntegers(0, 25); • Native support for JS Typed Arrays • Access the values efficiently from Java
  18. 18. Workers • Supports multi-threaded JavaScript execution • Implemented as Java Threads • Each JS Script runs in its own ‘Isolate’ • Can send messages between threads
  19. 19. Debugger Support • J2V8 can listen on a port for debug events
  20. 20. Performance var data = []; for (var i = 0; i < 100000; i++) { data[i] = i; } for ( var i = 0; i < data.length; i++) { for ( var j = 0; j < data.length; j++ ) { if ( data[i] < data[j] ) { var tmp = data[i]; data[i] = data[j]; data[j] = tmp; } } }
  21. 21. Performance V8 Nashorn Rhino Rhino: ??? Nashorn 2:30 J2V8 0:13
  22. 22. Performance Considerations • Minimize callbacks from JavaScript to Java • ~4000 Per Second on my MBP • Use bulk array copy to move primitives from JS to Java • 60fps in our animation demo
  23. 23. Tern • Tern is a stand-alone code-analysis engine for JavaScript • Moved to J2V8 for Java integration
  24. 24. Building V8 • Embeds V8 in the .jar file • V8 built for each platform we support • Build instructions encoded as a docker file [%] git clone https://github.com/eclipsesource/J2V8.git
 [%] docker build --rm=true --no-cache -t "eclipsesource/v8-build" .
  25. 25. Resources • Getting started with J2V8 • Registering Java Callbacks with J2V8 • Implementing WebWorkers with J2V8 • Multithreaded JavaScript with J2V8 • Using J2V8 with Heroku • All linked from our GitHub Page
  26. 26. Future Work • Script API (JSR 223) • Advanced exception handling between Java and JS • Batch callbacks for better JNI performance • Debugger integration with Chrome Dev Tools
  27. 27. Using J2V8 • J2V8 is available in Maven Central • Currently 5 variants are available: com.eclipsesource.j2v8.j2v8_win32_x86:2.2.1
 com.eclipsesource.j2v8.j2v8_macosx_x86_64:2.2.1
 com.eclipsesource.j2v8.j2v8_android:2.2.1
 com.eclipsesource.j2v8.j2v8_android_armv7l:2.2.1
 com.eclipsesource.j2v8.j2v8_android_x86:2.2.1 • j2v8_android:2.0 contains both x86 and armv7l, and the correct library will be selected at runtime
  28. 28. J2V8 • Open Source Java bindings for V8 • Licensed under the EPL • For J2V8 news, follow me on Twitter @irbull https://github.com/eclipsesource/j2v8

×