This is sort of expected, as JavaScript can indeed be equally fast as native code, but only when it is in a “hot path,” i.e. heavily optimized by JIT compilers.
前職の Bit Journey, Inc. では3年ほどKibelaのサーバーサイドやフロントエンドアプリの開発に関わりました。Bit Journey在職中に子供がうまれ、現在も夫婦で分担しながら子育てをしていますが、この子育て初期という大変な時期*1にBit Journeyで気持ちよく働けたのはたいへんな僥倖でした。ここで改めて感謝いたします。
As long as the JSON string is only evaluated once, the JSON.parse approach is much faster compared to the JavaScript object literal, especially for cold loads.
# frozen_string_literal: truemoduleTypesclassDateTime < GraphQL::Schema::Scalar
description "A datetime type, encoded in ISO 8601 string in JSON, or timestamp type in MessagePack"# @param value [ActiveSupport::TimeWithZone]# @return [String]defself.coerce_result(value, _ctx)
value # use to_json or equivalent converterend# @param value [String,ActiveSupport::TimeWithZone]# @return [ActiveSupport::TimeWithZone]defself.coerce_input(value, _ctx)
if value.is_a?(ActiveSupport::TimeWithZone)
value
elseTime.zone.parse(value)
endrescueArgumentErrornilendendend
MessagePackを単なるJSONの代替として考えるとJavaScript的にはメリットに乏しいのですが、JSONよりも表現が豊富で拡張性にすぐれたシリアライズフォーマットと捉えるとGraphQLと相性がいいなあ…ということを考えています。というわけで最近MessagePack for JavaScriptの実装を頑張っているのでした。まずは優れた実装がないと話になりませんからね。