Slide 1

Slide 1 text

+40/0CKFDUUJQT

Slide 2

Slide 2 text

Who Name 青野健利 GitHub @brn Twitter @brn227 What Dev Lead at 株式会社AI Shift Contributor of V8 Javascript Engine

Slide 3

Slide 3 text

+40/TUSJOHJGZ

Slide 4

Slide 4 text

JSON.stringify使えてますか? JSON.stringify JSON.stringifyはJSONを文字列化するだけ

Slide 5

Slide 5 text

͡Όͳ͍

Slide 6

Slide 6 text

JSON.stringifyの定義 JSON.stringify JSON.stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string;

Slide 7

Slide 7 text

JSON.stringifyの定義 JSON.stringify replacer?: (number | string)[] | null

Slide 8

Slide 8 text

replacerとは? JSON.stringify function replacer(key, value) { // ϓϩύςΟΛϑΟϧλʔ͢Δ if (typeof value === "string") { return undefined; } return value; } var foo = { foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7, }; JSON.stringify(foo, replacer); // '{"week":45,"month":7}'

Slide 9

Slide 9 text

replacer JSON.stringify JSONを文字列化する際に値を置き換えるだけ

Slide 10

Slide 10 text

͡Όͳ͍

Slide 11

Slide 11 text

replacerで巡回 JSON.stringify JSON.stringify({a: {b: {c: 1}}}, (...args) => {console.log(JSON.stringify(args));return args[1]}, "") // ["",{"a":{"b":{"c":1}}}] // ["a",{"b":{"c":1}}] // ["b",{"c":1}] // ["c",1] // '{"a":{"b":{"c":1}}}'

Slide 12

Slide 12 text

replacer JSON.stringify ネストした複雑なJSONを自動で巡回して 値を取得することが可能

Slide 13

Slide 13 text

UZQJB

Slide 14

Slide 14 text

JSON.stringifyは遅い JSON.stringify あらゆる構造のJSONに対応しているため

Slide 15

Slide 15 text

TypeScriptの型情報を使いたい JSON.stringify JSON.stringify早くできそう

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

TypeScriptの型情報を使いたい typia type User = { firstName: string; lastName: string; age: number; reg: RegExp; }; console.log( typia.json.stringify({ firstName: "Matteo", lastName: "Collina", age: 32, reg: /"([^"]|\\")*"/, }) );

Slide 18

Slide 18 text

なぜ早くなるのか typia 型情報を使って事前にコンパイルするから

Slide 19

Slide 19 text

console.log( ((input) => { const $io1 = (input) => "string" === typeof input.source && "boolean" === typeof input.global && "boolean" === typeof input.ignoreCase && "boolean" === typeof input.multiline && "number" === typeof input.lastIndex && "string" === typeof input.flags && "boolean" === typeof input.sticky && "boolean" === typeof input.unicode && "boolean" === typeof input.dotAll; const $string = typia.json.stringify.string; return `{"firstName":${$string(input.firstName)},"lastName":${$string( input.lastName )},"age":${input.age},"reg":${`{"source":${$string( input.reg.source )},"global":${input.reg.global},"ignoreCase":${ input.reg.ignoreCase },"multiline":${input.reg.multiline},"lastIndex":${ input.reg.lastIndex },"flags":${$string(input.reg.flags)},"sticky":${ input.reg.sticky },"unicode":${input.reg.unicode},"dotAll":${input.reg.dotAll}}`}}`; })({ firstName: "Matteo", lastName: "Collina", age: 32, reg: /"([^"]|\\")*"/, }) );

Slide 20

Slide 20 text

他にも色々あります typia // RUNTIME VALIDATORS export function is(input: unknown): input is T; // returns boolean export function assert(input: unknown): T; // throws TypeGuardError export function validate(input: unknown): IValidation; // detailed // JSON FUNCTIONS export namespace json { export function application(): IJsonApplication; // JSON schema export function assertParse(input: string): T; // type safe parser export function assertStringify(input: T): string; // safe and faster } // PROTOCOL BUFFER export namespace protobuf { export function message(): string; // Protocol Buffer message export function assertDecode(buffer: Uint8Array): T; // safe decoder export function assertEncode(input: T): Uint8Array; // safe encoder } // RANDOM GENERATOR export function random(g?: Partial): T;

Slide 21

Slide 21 text

TUSVDUVSFE$MPOF

Slide 22

Slide 22 text

Objectのコピー structuredClone JSON.stringifyとparse使うとできるけど..

Slide 23

Slide 23 text

JSONを経由したクローン方法 structuredClone JSON.parse(JSON.stringify({a:1}))

Slide 24

Slide 24 text

簡単にできる structuredClone structuredClone({a: 1})

Slide 25

Slide 25 text

Structured Clone の仕様 structuredClone • Function オブジェクトは構造化複製アルゴリズムでは複製されません。複製しようとすると DataCloneError 例外が 発生します。 • DOM ノードを複製するときも同様に DataCloneError 例外発生します。 • 一部のオブジェクトのプロパティは保持されません。 • RegExp オブジェクトの lastIndex フィールドは保持されません。 • プロパティ記述子、セッター、ゲッター(もしくは同様のメタデータ系機能)は複製されません。 たとえば、あるオブジェク トがプロパティ記述子によって読み取り専用になっている場合でも、複製したものでは既定の条件である読み取り/書 き込みに変わります。 • プロトタイプチェーンは探索、複製されません。

Slide 26

Slide 26 text

Structured Clone の仕様 structuredClone https://developer.mozilla.org/ja/docs/Web/API/Web_Workers_API/Structured_clone_algorithm