Skip to content

Instantly share code, notes, and snippets.

@Phrogz
Created January 7, 2014 18:20
Show Gist options
  • Save Phrogz/8303990 to your computer and use it in GitHub Desktop.
Save Phrogz/8303990 to your computer and use it in GitHub Desktop.
local lua = [[{
["hello"]="there\nworld",
["array"]={1,2,3,1e6,{["more"]=false},nil,true},
["more"]={
["hello"]="there\nworld",
["array"]={1,2,3,1e6,{["more"]=false},nil,true},
["more"]={
["hello"]="there\nworld",
["array"]={1,2,3,1e6,{["more"]=false},nil,true},
["more"]={
["hello"]="there\nworld",
["array"]={1,2,3,1e6,{["more"]="\"And\" \"actually,\" \"doesn't\" \"hurt\" \"to\" \"benchmark\" \"both,\" \"huh?\" \":)\""},nil,true}
}
}
}
}]]
local json = lua:gsub('%["','"'):gsub('"%]','"'):gsub('=',':'):gsub('nil','null'):gsub('{1','[1'):gsub('true}','true]')
N = 100000
local start = os.clock()
for i=1,N do loadstring('return '..lua)() end
local duration = os.clock() - start
print("Lua native:",duration,"seconds") --> 3.009 seconds
-- Using https://github.com/harningt/luajson v1.3.2
package.path = package.path .. ';./luajson/lua/?.lua'
local JSON = require 'json'
local start = os.clock()
for i=1,N do JSON.decode(json) end
local duration = os.clock() - start
print("LuaJSON:",duration,"seconds") --> 16.925 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment