Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save doha99-eh/bfcd9e073c03c403ff7fe1e9315d1ca5 to your computer and use it in GitHub Desktop.

Select an option

Save doha99-eh/bfcd9e073c03c403ff7fe1e9315d1ca5 to your computer and use it in GitHub Desktop.

Revisions

  1. doha99-eh revised this gist Apr 28, 2023. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions bm__JSON.parse_HWIA_deep_symbolize_keys_symbolized_name.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    # frozen_string_literal: true

    # gem install benchmark-memory
    # gem install benchmark-ips

    require 'benchmark/ips'
    require 'benchmark/memory'
    require 'active_support/all'
  2. @keymastervn keymastervn renamed this gist Sep 29, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @keymastervn keymastervn created this gist Sep 29, 2022.
    78 changes: 78 additions & 0 deletions 1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    # frozen_string_literal: true

    require 'benchmark/ips'
    require 'benchmark/memory'
    require 'active_support/all'

    TIMES = 50_000

    HASH = {
    "colour" => "red",
    "sizes" => [
    "measurements_1" => {
    "height" => 1,
    "length" => 2,
    "depth" => 3,
    "specs" => {
    "oval" => true,
    "patterns" => "french lily"
    }
    },
    "measurements_2" => {
    "height" => 10,
    "length" => 20,
    "depth" => 30,
    }
    ]
    }.to_json

    Benchmark.ips do |x|
    x.config(:time => 5, :warmup => 2)

    x.report("with_indifferent_access:") { JSON.parse(HASH).with_indifferent_access[:sizes][0][:measurements_1][:specs] }
    x.report("deep_symbolize_keys:") { JSON.parse(HASH).deep_symbolize_keys[:sizes][0][:measurements_1][:specs] }
    x.report("symbolize_names:") { JSON.parse(HASH, symbolize_names: true)[:sizes][0][:measurements_1][:specs] }

    x.compare!
    end

    # Warming up --------------------------------------
    # with_indifferent_access:
    # 7.030k i/100ms
    # deep_symbolize_keys: 7.100k i/100ms
    # symbolize_names: 17.229k i/100ms
    # Calculating -------------------------------------
    # with_indifferent_access:
    # 67.537k (± 5.2%) i/s - 337.440k in 5.010073s
    # deep_symbolize_keys: 79.691k (± 3.6%) i/s - 404.700k in 5.085082s
    # symbolize_names: 166.636k (± 8.6%) i/s - 826.992k in 5.037052s

    # Comparison:
    # symbolize_names:: 166635.5 i/s
    # deep_symbolize_keys:: 79691.3 i/s - 2.09x (± 0.00) slower
    # with_indifferent_access:: 67537.1 i/s - 2.47x (± 0.00) slower

    Benchmark.memory do |x|
    x.report("with_indifferent_access:") { JSON.parse(HASH).with_indifferent_access[:sizes][0][:measurements_1][:specs] }
    x.report("deep_symbolize_keys:") { JSON.parse(HASH).deep_symbolize_keys[:sizes][0][:measurements_1][:specs] }
    x.report("symbolize_names:") { JSON.parse(HASH, symbolize_names: true)[:sizes][0][:measurements_1][:specs] }

    x.compare!
    end

    # Calculating -------------------------------------
    # with_indifferent_access:
    # 4.960k memsize ( 504.000 retained)
    # 38.000 objects ( 3.000 retained)
    # 12.000 strings ( 1.000 retained)
    # deep_symbolize_keys: 5.640k memsize ( 1.280k retained)
    # 55.000 objects ( 8.000 retained)
    # 12.000 strings ( 2.000 retained)
    # symbolize_names: 4.152k memsize ( 0.000 retained)
    # 37.000 objects ( 0.000 retained)
    # 12.000 strings ( 0.000 retained)

    # Comparison:
    # symbolize_names:: 4152 allocated
    # with_indifferent_access:: 4960 allocated - 1.19x more
    # deep_symbolize_keys:: 5640 allocated - 1.36x more