in reply to Combine 2 JSON files

You didn't provide the second JSON, so I had to hallucinate one myself. The comment shows where you need to adjust your code.

The basic idea is to index the first json by the external id using a hash.

#!/usr/bin/perl use warnings; use strict; use experimental qw( signatures ); use Cpanel::JSON::XS qw{ encode_json decode_json }; use List::Util qw{ shuffle }; sub create_json1($name, $size) { my @watchers; for my $i (shuffle(1 .. $size)) { push @watchers, { externalId => $i, watchers => [map "$_", map 1 + int rand $size, 0 .. ra +nd 4] }; } open my $out, '>', $name or die "$name: $!"; print {$out} encode_json(\@watchers); } sub create_json2($name, $size) { my @list; for my $i (shuffle(1 .. $size)) { push @list, { id => $i }; } open my $out, '>', $name or die "$name: $!"; print {$out} encode_json(\@list); } sub merge ($name1, $name2) { open my $in1, '<', $name1 or die "$name1: $!"; my $struct1 = decode_json(do { local $/; <$in1> }); open my $in2, '<', $name2 or die "$name2: $!"; my $struct2 = decode_json(do { local $/; <$in2> }); my %by_id = map { $_->{externalId}, $_->{watchers} } @$struct1; for my $member (@$struct2) { # This depends on the structure of the 2nd file. if (exists $by_id{ $member->{id} }) { $member->{watchers} = $by_id{ $member->{id} }; } else { warn "$member->{id} not found in $name1"; } } print encode_json($struct2); } my $SIZE = 100_000; create_json1('1.json', $SIZE); create_json2('2.json', $SIZE); merge('1.json', '2.json');

Runs under 2 seconds on my old machine.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]