Skip to content

Commit

Permalink
Apply Martin's optimizations to _.is_equal
Browse files Browse the repository at this point in the history
  • Loading branch information
mirven committed Dec 13, 2009
1 parent d8d7c93 commit 79cc736
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/underscore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,14 @@ function Underscore.funcs.is_empty(obj)
return next(obj) == nil
end

-- Lifted from penlight's deepcompare() -- http://luaforge.net/projects/penlight/
-- Originally based on penlight's deepcompare() -- http://luaforge.net/projects/penlight/
function Underscore.funcs.is_equal(o1, o2, ignore_mt)
local ty1 = type(o1)
local ty2 = type(o2)
if ty1 ~= ty2 then return false end

-- non-table types can be directly compared
if ty1 ~= 'table' and ty2 ~= 'table' then return o1 == o2 end
if ty1 ~= 'table' then return o1 == o2 end

-- as well as tables which have the metamethod __eq
local mt = getmetatable(o1)
Expand All @@ -342,11 +342,11 @@ function Underscore.funcs.is_equal(o1, o2, ignore_mt)

for k1,v1 in pairs(o1) do
local v2 = o2[k1]
if v2 == nil or not is_equal(v1,v2) then return false end
if v2 == nil or not is_equal(v1,v2, ignore_mt) then return false end
end
for k2,v2 in pairs(o2) do
local v1 = o1[k2]
if v1 == nil or not is_equal(v1,v2) then return false end
if v1 == nil then return false end
end
return true
end
Expand Down

0 comments on commit 79cc736

Please # to comment.