Last active
August 29, 2015 14:26
-
-
Save jgaskins/42a7f95d2cab130ac0fc to your computer and use it in GitHub Desktop.
Benchmark methods with splat args in Opal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'opal' | |
class Foo | |
def opal_no_args_block(*args, &block) | |
end | |
%x{ | |
var TMP_3; | |
def.$js_no_args_block = TMP_3 = function() { | |
var self = this; | |
var $iter = TMP_3.$$p; | |
var block = $iter || nil; | |
var args = $slice.call(arguments, 0); | |
TMP_3.$$p = null; | |
}; | |
} | |
def opal_2_args(a, b, *args) | |
end | |
%x{ | |
def.$js_2_args = function(a, b) { | |
var self = this; | |
var args = $slice.call(arguments, 2); | |
}; | |
} | |
def opal_2_args_block(a, b, *args, &block) | |
end | |
%x{ | |
var TMP_4; | |
def.$js_2_args_block = TMP_4 = function(a, b) { | |
var self = this, $iter = TMP_4.$$p, block = $iter || nil; | |
var args = $slice.call(arguments, 2); | |
TMP_4.$$p = null; | |
} | |
} | |
end | |
DURATION = 10 | |
def iteration_count message, method | |
start = Time.now | |
count = 0 | |
while Time.now - start < DURATION | |
yield | |
count += 1 | |
end | |
`document.write("<p>" + message + ": " + count + "</p>")` | |
end | |
foo = Foo.new | |
count = 0 | |
start = Time.now | |
while Time.now - start < DURATION | |
foo.opal_no_args_block {} | |
count += 1 | |
end | |
`document.write("<p>Opal Default, no args, capture block: " + count + "</p>")` | |
count = 0 | |
start = Time.now | |
while Time.now - start < DURATION | |
foo.js_no_args_block {} | |
count += 1 | |
end | |
`document.write("<p>Optimized JS, no args, capture block: " + count + "</p>")` | |
count = 0 | |
start = Time.now | |
while Time.now - start < DURATION | |
foo.opal_2_args 1, 2 | |
count += 1 | |
end | |
`document.write("<p>Opal Default, two args, no block: " + count + "</p>")` | |
count = 0 | |
start = Time.now | |
while Time.now - start < DURATION | |
foo.js_2_args 1, 2 | |
count += 1 | |
end | |
`document.write("<p>Optimized JS, two args, no block: " + count + "</p>")` | |
count = 0 | |
start = Time.now | |
while Time.now - start < DURATION | |
foo.opal_2_args_block(1, 2) {} | |
count += 1 | |
end | |
`document.write("<p>Opal Default, two args, capture block: " + count + "</p>")` | |
count = 0 | |
start = Time.now | |
while Time.now - start < DURATION | |
foo.js_2_args_block(1, 2) {} | |
count += 1 | |
end | |
`document.write("<p>Optimized JS, two args, capture block: " + count + "</p>")` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment