rubyjs / therubyracer

Embed the V8 Javascript Interpreter into Ruby
1.67k stars 190 forks source link

Change names access helper to iterate over the ancestors instead of doing dup/shift #410

Open TikiTDO opened 8 years ago

TikiTDO commented 8 years ago

Due to setup of the helper calling the get or set methods on any js object bound to a ruby object will call the accessibe_names each time. This will result in the duplication of the ancestors array which will be used for iteration and then discarded, which is quite inefficient if you plan on calling into ruby often.

Note, an identical and even faster effect could be achieved by refactoring the class to testing where a method is defined using a process like this:

def method_below_object
  begin
    ancestors = obj.class.ancestors
    index_of_object = ancestors.index(Object)
    index_of_method_owner = ancestors.index(obj.method(name).owner)
    return index_of_method_owner < index_of_object
  rescue
    return false
  end
end

I can implement the change if there is interest.