perf(fromJSON): 40% perf increase by fixing "forin not optimized case"

https://github.com/petkaantonov/bluebird/wiki/Optimization-killers
This commit is contained in:
Ben Gotow 2015-11-05 20:16:01 -08:00
parent a77ba2b15d
commit 4dfffae083
2 changed files with 36 additions and 8 deletions

View file

@ -1,9 +1,25 @@
Message = require '../../src/flux/models/message'
Thread = require '../../src/flux/models/thread'
Label = require '../../src/flux/models/label'
{Utils} = require 'nylas-exports'
_ = require 'underscore'
describe 'Thread', ->
describe 'serialization performance', ->
xit '1,000,000 iterations', ->
iterations = 0
json = '[{"client_id":"local-76c370af-65de","server_id":"f0vkowp7zxt7djue7ifylb940","object":"thread","account_id":"1r6w6qiq3sb0o9fiwin6v87dd","snippet":"http://itunestandc.tumblr.com/tagged/itunes-terms-and-conditions/chrono _______________________________________________ http://www.macgroup.com/mailman/listinfo/smartfriends-chat","subject":"iTunes Terms And Conditions as you\'ve never seen them before","unread":true,"starred":false,"version":1,"folders":[],"labels":[{"server_id":"8cf4fn20k9pjjhjawrv3xrxo0","name":"all","display_name":"All Mail","id":"8cf4fn20k9pjjhjawrv3xrxo0"},{"server_id":"f1lq8faw8vv06m67y8f3xdf84","name":"inbox","display_name":"Inbox","id":"f1lq8faw8vv06m67y8f3xdf84"}],"participants":[{"name":"Andrew Stadler","email":"stadler@gmail.com","thirdPartyData":{}},{"name":"Smart Friends™ Chat","email":"smartfriends-chat@macgroup.com","thirdPartyData":{}}],"has_attachments":false,"last_message_received_timestamp":1446600615,"id":"f0vkowp7zxt7djue7ifylb940"}]'
start = Date.now()
while iterations < 1000000
if _.isString(json)
data = JSON.parse(json)
object = new Thread()
object.fromJSON(data)
object
iterations += 1
console.log((Date.now() - start) / 1000.0 + "ms per 1000")
describe '.sortLabels()', ->
getSortedLabels = (inputs) ->
labels = _.map inputs, (i) ->

View file

@ -67,8 +67,13 @@ class Model
values["clientId"] ?= values["id"]
else
values["serverId"] ?= values["id"]
for key, definition of @attributes()
@[key] = values[key] if values[key]?
@constructor.attributesKeys ?= Object.keys(@constructor.attributes)
for key in @constructor.attributesKeys
continue if key is 'id'
continue unless values[key]?
@[key] = values[key]
@clientId ?= Utils.generateTempId()
@
@ -94,9 +99,15 @@ class Model
# This method is chainable.
#
fromJSON: (json) ->
# Note: The loop in this function has been optimized for the V8 'fast case'
# https://github.com/petkaantonov/bluebird/wiki/Optimization-killers
#
if json["id"] and not Utils.isTempId(json["id"])
@serverId = json["id"]
for key, attr of @attributes()
@constructor.attributesKeys ?= Object.keys(@constructor.attributes)
for key in @constructor.attributesKeys
continue if key is 'id'
attr = @constructor.attributes[key]
@[key] = attr.fromJSON(json[attr.jsonKey]) unless json[attr.jsonKey] is undefined
@
@ -110,11 +121,12 @@ class Model
#
toJSON: (options = {}) ->
json = {}
for key, attr of @attributes()
value = attr.toJSON(@[key])
if attr instanceof Attributes.AttributeJoinedData and options.joined is false
continue
json[attr.jsonKey] = value
@constructor.attributesKeys ?= Object.keys(@constructor.attributes)
for key in @constructor.attributesKeys
continue if key is 'id'
attr = @constructor.attributes[key]
continue if attr instanceof Attributes.AttributeJoinedData and options.joined is false
json[attr.jsonKey] = attr.toJSON(@[key])
json["id"] = @id
json