V8 version 0.5: typed arrays and sql.js

[This article was first published on OpenCPU, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

opencpu logo

Earlier this month, V8 version 0.5 appeared on CRAN. This version adds support typed arrays as specified in ECMA 6 in order to support high performance computing and libraries compiled with emscripten. A big thanks goes to Kenton Russell (@timelyportfolio) for suggesting these features.

Example: sql.js

These new features increase the amount of JavaScript libraries that will run out-of-the-box on V8. For example, sql.js is a port of SQLite to JavaScript, by compiling the SQLite C code with Emscripten:

# Load V8
library(V8)
stopifnot(packageVersion("V8") >= "0.5")

# Create JavaScript context and load sql.js
ct <- new_context()
ct$source("https://raw.githubusercontent.com/kripken/sql.js/master/js/sql.js")
 
# Evaluate JavaScript code
ct$eval('
var db = new SQL.Database()
db.run("CREATE TABLE hello (person char, age int);")
db.run("INSERT INTO hello VALUES ('jerry', 34);")
db.run("INSERT INTO hello VALUES ('mary', 27);")
db.run("INSERT INTO hello VALUES ('joe', 65);")
db.run("INSERT INTO hello VALUES ('anna', 18);")

// query:
var out = []
var stmt = db.prepare("SELECT * FROM hello WHERE age < 40");
while (stmt.step()) out.push(stmt.getAsObject());
')
 
# Copy the object from JavaScript to R
data <- ct$get("out")
print(data)

More V8 fun

Several other examples are available on gist, for example cheerio (html parsing), turf.js (geojson), viz.js and KaTeX. I am working on several packages that implement actual bindings to JavaScript libraries using V8. The first ones have just landed on CRAN: minimist and js.

To learn more, have a look at the vignettes:

Questions, suggestions? Find me on twitter or github.

To leave a comment for the author, please follow the link and comment on their blog: OpenCPU.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)