►
From YouTube: A Crash Course On Worker Threads - Rich Trott, University of California, San Francisco
Description
Speakers: Rich Trott
Node.js 10.5.0 introduced `worker_threads`, enabling the creation of simple multi-threaded applications in JavaScript. This may sound daunting if you've never dealt with threads before in another language. But JavaScript threads are pretty simple and, very importantly, fun. This talk will be a quick introduction to the fun of worker_threads.
I promise not to make any "you got your multi-threaded chocolate in my single-threaded JavaScript peanut butter" jokes.
A
It's
three
o'clock
and
I
believe
in
starting
promptly.
So
let's
do
this.
So
if
you
come
here
to
see
worker
threads
crash
course
you're
in
the
right
place
and
thank
you
but
I
do
question
your
judgment
because
Joey
Cheung
is
giving
a
talk
in
the
other
room
about
how
no
bootstraps
itself
and
she's
awesome
and
it's
gonna,
be
amazing.
Talk
and
it's
totally
where
I
want
to
be
right
now,
but
I'm
here,
just
I
mean
I'm
happy
to
be
here
too.
A
A
You've
made
the
wrong
choice
to
be
here
so
hello,
friends,
thank
you
and
I
talk
fast
I'll
try
not
to,
but
actually
I
won't
try
not
to
because
I
have
a
half
hour
and
I
ran
through
this
talk
about
an
hour
ago,
and
it
was
way
over.
So
I
cut
a
bunch
of
stuff
out,
mostly
jokes,
though
so
don't
worry
about
it.
It
just
won't
be
as
funny
as
I
wanted
it
to
be,
but
hopefully
I'll
have
as
much
material.
A
My
name
is
rich
I
work
for
the
University
of
California
at
their
San
Francisco
campus
at
the
library
right
about
now
is
when
I
imagine
people
going
he
works
in
the
library.
What
is
he
doing
here
in
serve
a
Jim
Gaffigan
voice?
That
just
makes
me
relax
and
feel
good
about
things,
but
anyway,
as
it
happens,
I
do
a
lot
of
work
on
node,
core
and
I'm.
Also
the
author
of
a
rock
opera
about
a
steak
house,
I,
don't
know
if
the
sound
on
this
is
plugged
in.
A
But
let's
see
if
this
works
here,
I
don't
know
okay,
so
it's
coming
out
of
the
projector
I.
Think,
okay
cool.
So
anyway,
I
didn't
come
here
to
play
examples
of
the
rock
opera,
but
if
you
you
know,
but
I
did
want
to
point
it
out,
because
all
of
the
slot,
although
like
links
to
blog
posts
and
documentation
and
videos
and
stuff,
are
at
pals
family,
say
guys.
A
Calm
I,
don't
think
I
have
a
link
to
the
slide
deck
which
I
guess
I'll,
add
after
I'm
done
here,
okay
anyway,
but
I'm
not
here
to
talk
about
that
stuff.
I'm
here
to
talk
about
node,
but
first
some
disclaimers
the
views
expressed
on
my
own
and
not
necessarily
those
of
my
employer.
That's
a
pretty
standard
disclaimer
these
expressed
in
my
own
necessarily
those
of
node.
There
are
a
lot
of
other
people
involved
in
node.
A
few
of
them
are
here
right
now.
A
A
So
use
no
twelve.
If
you
don't
want
to
use
a
command
line
flag,
which
you
don't
specifically,
here's
no
12,
11,
zero
or
newer,
although
I
don't
think
there
is
a
newer
version
in
the
12
release
line,
there's
13
dot
as
well
out,
but
anyway,
that's
the
first
version.
12
11
0
is
the
first
LTS
version
where
support
is
officially
stable
rather
than
experimental.
So
yeah,
oh
no,
it's
whoa,
13
1,
because
the
time
of
this
writing
it's
the
most
recent
yes,
not
12,
3
12
13
one
is
the
most
recent
great
ok
super.
A
So
anyway,
worker
threads
hey.
What
are
they
so
they're
kind
of
like
web
workers,
but
different,
for
example,
if
you're
used
to
web
workers
there's
shared
workers,
no
such
thing
here,
but
they're
also
kind
of
like
threads
and
other
programming
languages,
but
not
if
you
use
threads
and
other
programming
languages
cool.
If
you
haven't
use
threads
and
other
program
names,
just
cool,
it's
gonna,
be
fine,
don't
worry
so
JavaScript
right!
We
all
love
JavaScript
and
we
all
know
JavaScript
is
single
threaded,
even
if
you
have
no
idea
what
that
means.
A
Before
you
saw
this
slide,
there's
a
100%
chance
that
you've
heard
the
phrase.
Javascript
is
single
threaded,
because
javascript
is
single
threaded.
Arguably
I.
Actually
don't
want
to
have
that
argument,
though.
So
the
point
is
that
your
code
is
runs.
You
know
it
can
do
one
thing
at
a
time.
So
it's
why
this
program
never
exits.
A
There's
only
one
execution
thread
handling
this
code,
so
the
code
in
the
set
timeout,
which
would
break
us
out
of
the
while
loop,
never
gets
to
run
because
the
code
instead
timeout
won't
run
until
the
while
loop
relinquishes
control
of
the
event
loop.
So
this
is
gonna
run
forever
or
until
you
hit
control-c
or
until
you
turn
off
your
computer
or
whatever,
but
it
won't
exit
cleanly
and
whatever
causes
is
to
exit
it's
not
going
to
be
that
set
timeout.
This
is
called
blocking
the
event
loop.
A
If
you
already
understand
what
that
means
great,
if
you
don't
trust
me
and
look
it
up
later,
there
are
some
good
YouTube
videos
of
talks
about
it.
My
favorite
is
what
the
heck,
what
the
heck
is
the
event
loop
anyway
by
Phillip
Roberts
I,
think
that
was
a
gist
confit.
You
I,
don't
know
anyway,
there's
a
link
at
pal's
family
steakhouse,
calm.
Now
you
may
be
thinking
hey.
This
is
cool
everything,
but
notice
asynchronous
I.
A
So
sharing
large
amounts
of
data
is
often
problematic
and
each
process
consumes
the
full
amount
of
RAM
required
by
node.
This
can
be
inefficient,
although
again,
if
it's
working
for
you,
then
great,
but
it
doesn't
work
great
for
a
lot
of
things
and
even
for
things
that
it
does
work
for.
Worker
threads
will
often
be
better
because
worker
threads
are
more
lightweight
and
they
are
better
at
sharing
data.
So,
let's
dive
in
here's
a
hello
world
example
and
we're
gonna
go
through
it
step
by
step.
A
The
first
line
pulls
in
three
things
from
the
worker
threads
module:
it
pulls
in
the
worker
class,
the
is
main
thread
boolean
and
the
parent
port
object.
The
worker
class
will
be
familiar
to
you
if
you've
used
web
workers.
If
you
haven't
used
web
workers,
there's
there's
a
great
blogger
about
the
worker
class
and
that's
Karl
Marx,
but
we'll
explain.
The
worker
class
along
with
is
main
thread
and
param
port
laid
at
parent
port
later
just
know
that
they
all
came
from
the
worker
thread,
module
and
we'll
start
with
this
main
thread.
A
We
use
it
to
make
sure
we're
not
inside
a
worker
thread
we're
checking
that
we're
in
the
main
threats
that
we
know
it's:
okay,
to
launch
a
worker
thread.
If
we
didn't
do
this
check,
then
then
this
code
might
launch
a
worker
thread
that
launches
a
worker
thread
that
launches
a
worker
thread
ad
infinitum,
or
until
you
run
out
of
you,
know,
stack
space
or
something
anyway,
I,
don't
know
what
you
have
actually
I'd
be
a
good
experiment.
A
So
there
you
go
so
we
we
know
we're
in
the
main
thread.
So
what
we're
gonna
do
is
create
a
worker
thread,
so
we
use
the
constructor
for
the
worker
class.
That's
what
new
worker
is
right
and
we
pass
it
underbar
underbar
file
name,
which
is
you
probably
know,
is
the
special
node
variable
that
contains
the
name
of
the
file
currently
being
executed.
And
if
you
didn't
know
it
now,
you
do,
you
can
create
a
worker
thread
to
run
any
JavaScript
file.
A
Although
to
do
that,
you
need
to
pass
a
second
argument
to
the
constructor
that
tells
it
that
you're
doing
that
I
tend
to
avoid
that,
because
string
as
blob
of
executable
code
raises
the
same
kind
of
hives
that
it
might
raise
for
eval,
because
that's
basically
what
it
is,
but
it's
an
option
if
it
makes
sense
for
your
use
case,
so
we're
creating
a
worker.
Now,
let's
listen
for
messages
from
the
worker.
This
is
the
usual
event
listener
syntax
in
node.
Remember
we're
in
the
main
thread.
A
Still
not
the
worker
thread
we're
listening
for
messages,
events
on
the
worker
we've
created
and
when
we
get
one
we're
going
to
use,
console
dot
log
to
print
the
message
and
that's
it
for
the
main
thread.
Remember
we
were
in
an
if
block
that
checked
if
we
were
in
the
main
thread.
So
now,
let's
use
else
and
do
the
right
stuff
for
when
we're
in
the
worker
thread,
and
the
only
thing
we're
gonna
do
is
use
parent
port
to
send
the
message
using
its
post
message.
A
Method
in
the
main
thread,
parent
port
will
be
null,
but
so
if
we
want
to
send
a
message
to
the
worker
from
the
main
thread,
we
use
the
post
message
method.
That's
on
the
worker
instance,
but
in
a
worker
thread,
parent
port
dot
post
message
can
be
used
to
send
messages
to
the
main
thread.
So
let's
use
it
to
send
the
message
that
says
hello
world
and
that's
the
end
of
the
file.
You'll.
Remember
that
in
the
main
thread
we
had
a
listener
for
messages
from
the
worker
and
that
listener
prints
the
message.
A
So
if
you
run
this
file,
it
will
print
hello
world
not
terribly
useful.
There
are
much
easier
ways
to
do
that,
but
it
does
introduce
the
very
basic
concepts
of
worker
threads.
So
now,
let's
do
something
just
as
contrived
as
this
but
more
interesting.
Perhaps
you
remember
the
game:
six
degrees
of
Kevin
Bacon.
If
not,
it's
simple,
given
the
name
of
any
actor
in
a
film,
your
job
is
to
connect
them
to
Kevin
Bacon
in
six
or
fewer
steps
in
the
following
manner.
A
Let's
say:
you're
challenged
to
connect
Katie
Boeck,
Katy
Perry
to
Kevin
Bacon
in
six
or
fewer
steps.
Katy
Perry
was
in
Zoolander
2
with
John
Malkovich
and
John.
Malkovich
was
in
Queens
logic,
with
Kate,
with
Kevin
Bacon
boom
Katy
Perry
to
Kevin
Bacon
in
two
steps,
I've
seen
neither
of
those
movies.
There
are
already
websites
that
solve
six
degrees
of
Kevin
Bacon
by
using
IMDB
data
several
years
ago.
A
I
wanted
to
do
this
for
musicians
playing
on
recordings
of
individual
songs,
so
I
made
a
site
called
music
routes,
but
it's
been
broken
for
a
long
long
time.
So,
let's
fix
it.
First
surprised
surprising,
there's
no
usable
database
available
for
what
musicians
playing
what
tracks
and
love
people
think
Allmusic
has
it
all.
Music
has
album
data
but
not
track
data.
A
lot
of
people
think
musicbrainz
a
lot
of
information,
but
it
has
artist
data
and
not
individual
data,
and
a
lot
of
people
think
discogs
has
that
information
discogs
just
copies.
A
What's
ever
on
the
album
sleeve,
which
means
that,
for
example,
it
will
tell
you
that
that
Rudy
Sarzo
played
bass
on
Ozzy
Osbourne
Skyrim
madman,
as
everybody
knows
he
did,
and
he
was
just
on
the
in
the
credits,
leaked
Hurst,
Lake
played
drums
and
Bob
Daisley
played
bass.
That's
the
end
of
Ozzy
Osbourne
information
for
this
talk,
but
you
know
y'all
know
just
a
little
bit
more
now,
but
I
was
born
so
anyway.
A
That
brings
us
to
wiki
data,
which
has
some
data
along
these
lines,
but
less
than
you
think-
and
you
know
that's
cool
its
wiki
data.
Everybody
can
add
information
to
it,
but
it's
also
very,
very,
very
unusually
slow
for
the
many
many
queries
we
will
need
to
make
in
our
searches.
So
I
built
my
own
database
and
published
it.
It's
very
incomplete,
but
it'll
do
for
here
and
I
also
built
a
rumor
rudimentary
a
little
visualizer
for
which
we
might
get
to
later.
I
don't
know
so.
A
In
order
to
solve
these
things,
we
could
use
breadth-first
search.
I
am
now
about
to
give
the
world's
worst
overview
of
breadth-first
search.
Here
it
goes.
Let's
go
back
to
connecting
Katy
Perry
to
Kevin
Bacon
step.
One
is
Katy,
Perry
Kevin
Bacon,
that's
a
JavaScript
triple
equal
there
in
the
middle.
The
answer,
obviously,
is
no
don't
be
ridiculous.
A
Step
two
find
everyone
that
was
in
a
movie
with
Katy
Perry.
Do
any
of
those
people
happen
to
be
Kevin
Bacon?
The
answer
is
no
includes
John,
Malkovich
and
other
people
step
three
find
everyone
that
was
in
a
movie
with
any
of
those
people
that
were
in
a
movie
with
Katy
Perry.
So
any
of
those
people
happen
to
be
Kevin
Bacon.
The
answer
is
yes,
so
we're
done.
Congratulations.
You've
just
witnessed
the
worst
explanation
of
breadth-first
search
ever
now.
Let's
do
a
slightly
better
explanation.
A
This
will
be
the
second
worst
explanation
of
breadth-first
search
ever
we're
going
to
connect
Katy,
Perry
and
Kevin
Bacon,
but
this
time
not
through
movies
this
time.
Let's
do
it
through
music
Kevin
Bacon
has
a
band
with
his
brother,
Michael
Bacon.
The
band
is
called
the
bacon
brothers
and
I'm.
Not
making
that
up.
Fun
fact
is:
you
can
see
this
photo
from
Wikipedia
Michael
Bacon's
nose
has
never
been
successfully
photographed.
A
So,
let's
see
if
we
can
connect
Katy
Perry
to
Kevin
Bacon
via
music,
so
step,
one
is
Katy.
Perry
Kevin
Bacon
no
get
out
of
here
with
that
nonsense.
So
here's
a
visualization
of
Katy
Perry
in
the
middle
and
everyone
she
recorded
with
on
our
album
one
of
the
boys,
which
I'm
sorry
to
say,
is
the
only
Katy
Perry
album
that
I
have
in
the
data
set.
A
You
can
open
up
or
requests
to
fix
that
if
you
want
to
correct
this
horrible
injustice
anyway,
Katy
Perry's
that
circle
in
the
middle,
like
I,
said
and
the
circle
each
circle
in
the
surrounding
ring
is
someone
who
is
one
step
away
from
Katy
Perry
because
they
played
with
her
on
the
album
so
somewhere
in
there
is
legendary
session
horn.
Minh
Jerry,
hey
there.
He
is
there's
also
your
rhythmic
guitar
Dave
Stewart
at
the
bottom,
who
goes
by
David
a
Stewart
because,
literally
and
I'm
not
making
this
up
either.
A
There
are
too
many
Dave
Stewart's
out
there.
There's
an
David
and
Stewart
and
in
there
so
notably
absent
from
evering,
though,
is
Kevin
Bacon.
So
now
imagine
we
take
each
of
those
circles
in
the
ring
around
Katy
Perry
and
we
find
out
everyone
who
is
recorded
with
each
of
these
people.
We
would
take
all
those
people
and
make
it
outer
ring
with
circles
of
each
of
them.
A
I
didn't
do
that,
but
I
did
mostly
because
well
for
a
lot
of
reasons,
one
I'm
lazy,
but
also
because
there
would
be
like
way
too
many
circles
to
fit
on
the
slide.
We're
gonna
get
to
that
in
a
minute,
but
yeah
the
number
of
circles
is
gonna,
grow
exponentially
or
exponentially
ish
with
each
ring
right.
So
you
won't
want
to
see
all
the
circles,
but
I
did
scroll
this
ugly
blue
line
sort
of
represent
that
outer
ring
kind
of
like
Saturn.
A
You
know
a
little
ring
around
the
planet
of
Katy
Perry
anyway
I'm
here
to
tell
you
something
exciting
about
that
outer
ring,
though
it
totally
contains
Kevin
Bacon,
it's
basic,
so
that's,
basically
breadth-first
search
here
are
the
results.
If
you
don't
believe
me
there,
you
go
yay
Jon,
Bon
Jovi,
who
would
have
thought
okay,
so
that
is
breadth-first
search.
A
Let's
implement
it.
No
just
kidding
for
purposes
of
this
presentation.
It's
an
implementation
detail
the
there
are
trade-offs,
various
ways
of
implementing
it
and
implementing
it.
I
don't
really
want
to
get
into
it
and
I.
Don't
I'm
I'm
talking
way
too
fast
as
to
this,
but
you
can
check
out
this
repository
for
how
I
implemented
it,
as
well
as
the
other
algorithms
we're
going
to
talk
about
in
a
little
bit.
The
important
thing
is
that
our
approach
will
keep
the
CPU
busy
rather
than
do
a
bunch
of
work
upfront.
A
This
is
so
that
we
can
see
how
cool
worker
threads
are,
but
it's
also
a
legitimate
trade-off
one
might
make
in
the
real
world.
It's
it's
not
always
worth
it
to
spend
time.
Upfront
pre-processing
your
data,
if
it's
time
consuming
takes
up
too
much
storage,
etc,
etc.
So
here's
what
breadth!
Here's
what
it
looks
like
we're,
let's
step
through
this,
the
first
line
gets
all
the
tracks
of
the
start:
person.
Sorry
for
the
long,
long
variable
names,
but
you
know
this.
A
It
made
sense
when
I
wrote
in
the
repository
anyway,
let's
say
it's
Aretha
Franklin
we're
gonna,
put
all
the
tracks
in
index
zero
of
an
array
of
tracks
for
the
start
person
the
index
indicates
how
many
steps
we've
gone
from
the
start
individual
and
this
line
populates
the
corresponding
array
of
individuals
that
are
in
the
source
of
those
tracks
above
so
in
this
case,
it's
an
array
of
just
one
individual
ID,
it's
just
a
wreath
of
Franklin
for
the
two
lines,
starting
here.
We're
gonna,
do
the
exact
same
thing
for
the
target
person.
A
Let's
say
it's
Carrie
Brownstein
this
line
checks
to
see
if
we
have
a
match
by
seeing
if
there
are
one
or
more
tracks
in
both
lists.
Lastly,
this
while
loop
runs
until
the
match
is
found,
this
line
adds
the
individuals
and
the
tracks
that
result
from
going
out
one
more
step
from
the
start
individual
than
we've
gone
this
far.
So
all
the
people
on
tracks
with
Aretha
Franklin,
then
the
next
time
it
runs.
A
It's
gonna
be
all
the
people
on
tracks
with
those
people
who
are
on
tracks,
with
the
reason,
Franklin
and
so
on,
and
so
on,
getting
exponentially
slower
as
more
and
more
data
is
involved
in
deduplication
and
queries.
So
this
line
updates
the
matches
list,
so
the
while
loop
will
stop
if
we
found
a
match
so
each
time
that
last
wild
loop
runs.
We
you
know
remember
each
of
these
rings
exponentially
ish,
more
work.
A
Longer
paths
will
take
longer,
and
so
you
know,
there's
a
bit
of
a
solution
hinted
at
at
a
use
of
an
otherwise
unnecessary
array
in
that
previous
code.
But,
first
of
all,
let's
check
how
breadth-first
search
to
performs
on
my
laptop
here's
a
run
with
the
results
it
took
more
than
14
seconds
just
to
do
the
breadth-first
search.
That's
a
lot
we
can
do.
We
can
do
better,
even
without
worker
threads,
by
doing
bi-directional
search
really
quickly.
Here's
how
bi-directional
search
works.
A
First
Katy
Perry
is
not
Kevin
Bacon,
despite
the
striking
resemblance
evident
in
that
photo
again.
We
look
for
every
one
that
is
connected
to
Katy
Perry
and
check
to
see
if
Kevin
Bacon's
in
there
he's
not.
Now
we
bounce
back
to
Kevin
Bacon
and
we
get
every
one
connected
to
him.
We
check
to
see
if
there
is
an
overlap
in
the
two
sets
of
musicians.
If
not,
then
we
do
another
query.
We
do
one
for
Katy
Perry's
cluster
of
people
still
no
match
to
one
for
Kevin.
Bacon's
cluster
people
still
know
Matt.
A
You
know
until
there's
a
match
now.
So,
let's,
let's
visualize
it
like
this.
Excuse
me
that
oval
on
the
left
is
Katy
Perry
and
that
oval
on
the
right
is
Kevin.
Bacon,
Katy,
Perry,
Kevin
Bacon
different
people
there's
space
between
them,
so
there's
a
bunch
of
little
dots,
but
those
are
all
people
just
like
Katie
Kevin
anyway.
Those
are
all
the
people
have
played
with
Katy
Perry,
still
no
Kevin
Bacon.
This
is
this
is
breadth-first
search.
We're
gonna
do
another
queen
us-korea
get
starts
to
get
expensive.
A
I
didn't
include
all
the
dots
that
should
be
in
there,
but
imagine
like
800
times
as
many
dots
and
still
no
Kevin
Bacon
we're
new
another
query
and
this
one's
even
more
expensive.
But
finally,
there
was
Kevin,
there's
Kevin,
Bacon,
alright,
so
now
bi-directional
search
would
go
this
way.
Let's
check.
Ok,
all
the
people
play
with
Katy
Perry
Kevin
Megan's,
not
in
there
all
people
play
with
Kevin
Bacon
Katy
Perry's,
not
in
there.
Oh
look
at
this.
We
do
one
more
query:
Kevin,
Bacon
and
Katy
Perry
have
have
some
overlap.
A
John
Bongiovi
or
someone
is,
is
in
the
middle
there,
Dave
Stewart
somebody
is
anyway
so
yeah.
So
you
know
we're
we're
we're
doing
fewer
expensive
queries,
we're
doing
the
same
number
of
queries,
because
we
have
to
do
as
many
queries
as
it
takes
to
can
to
get
the
number
of
steps
to
connect
the
two
people,
but
we're
doing
less
expensive
queries.
A
So,
let's
see
you
know
if
you
don't
believe
by
the
way,
if
you
need
Big
O
notation
to
show
the
how
that
works,
or
if
my
explanation
sucks
so
bad
that
you've
no
idea
what
I'm
talking
about
there's
a
Wikipedia
article
I
mean
so
alright.
How
am
I
doing
on
time?
I
got
12
minutes.
Okay,
bi-directional
search
is
just
like
breadth-first
search
right
down
to
the
comments,
except
for
the
contents
of
the
while
loop.
So
let's
go
ahead.
A
Let's
zoom
in
on
that,
while
loop,
you
can
see
that
we
do
a
breadth-first
search
starting
from
the
start
individual.
Then
we
check
if
there's
a
match
and
if
not,
we
do
a
second
breadth-first
search
starting
from
the
target
individual
and
we
repeat
until
we
find
some
overlap.
Holy-Moly,
we
went
from
14
seconds
to
less
than
3.
It's
awesome,
but
wait!
This
talk
is
about
worker
threads.
A
Why
be
bound
to
a
single
thread
rather
than
doing
one
breadth-first
search
over
here
and
checking
and
then
doing
another
one
over
here
and
checking
and
then
doing
another
one
over
here
and
show
you?
Why
not
just
run
two
threads
doing
simultaneous
breadth-first
searches
racing
to
see
which
one
you
know
can
return
and
overlapping
individual
first
so
to
create
our
worker
threads?
This
time
we
are
calling
new
worker
again
and
this
time
we're
putting
the
worker
code
in
a
file
called
worker.
A
This
allows
us
to
provide
the
idea
of
the
individual
to
start
with,
so
worker
data
gets
serialized
and
sent
over
to
the
worker,
which
then
uncie
realises
it
into
its
own
copy
of
the
data
and
which
is
basically
what
happens
when
you
post
MIT,
when
you
post
message
data
as
well
now,
worker
threats
can
do
this
awesome
magical
thing
where,
if
you
do
things
just
right,
you
can
share
memory
and
also
transfer
memory
buffers
between
the
main
thread
and
the
worker
thread.
A
Sharing
memory
doesn't
actually
resemble
sharing
nachos
like
this,
but
I
needed
an
image.
So
we're
not
doing
this
in
this
app
worker
data
just
sends
a
copy,
but
if
your
data
is
of
a
predictable
size
and
format,
and
if
there's
a
lot
of
it,
look
at
the
docs
for
information
on
sharing
memory
or
transferring
it,
it
will
be
useful
in
addition
to
the
shared
memory
stuff,
there's
pooling
for
this
application,
we
always
need
two
workers.
A
You
know
one
for
Katy
Perry
and
one
for
Kevin
Bacon,
and
we
don't
and
I
don't
care
about
the
cost
of
starting
one
up.
You
know
just
waiting
until
I
need
it
and
starting
one
up,
but
in
an
application
where
your
needs
are
more
dynamic
and
you're
trying
to
get
the
absolute
best
performance
you
can
you
want
to
investigate
having
a
pool
of
workers
that
you
use
as
needed?
There
are
NPM
modules
that
can
help
you,
if
you
want
to,
if
you
don't
want
to
implement
pooling
yourself
so
over
in
worker
j/s.
A
Reading
the
worker
data
value
is
done
like
this.
You
import
the
worker
data
property
from
the
built
in
worker
threads
module.
Then
read
the
data
and
then
read
the
value
of
the
ID
key,
we're
going
to
context
switch
back
to
the
main
thread.
We
have
an
error
listener
that
simply
throws
any
unexpected
errors
from
the
worker
and
we
have
a
callback
that
we
use
when
we
receive
a
message
from
the
worker.
The
index
here
is
used
to
distinguish
the
results
from
Katy
Perry's
worker
thread
and
Kevin
Bacon's
worker
thread,
so
we're
use
it.
A
So
we
might
use
0
for
Katy
Perry's
thread
and
1
for
Kevin
Bacon's
thread,
let's
head
over
to
the
worker
code
again
and
see
how
the
worker
invokes
the
callback.
So
each
worker
is
created,
it
grabs
all
the
tracks,
the
individuals
on
and
sends
them
along
the
individual
back
as
an
object
to
the
main
thread.
That
post
message
will
cause
the
callback
in
the
message
thread
to
execute.
So
here's
the
callback
and
again
the
index,
is
a
value
that
lets
us
use
the
same
callback
for
Katy
Perry's
tracks
as
we
used
for
Kevin
Bacon's
tracks.
A
A
It
removes
the
listeners
we
have
for
both
workers
and
then
it
calls
this
method.
That's
on
all
workers
called
terminate
and
what
terminate
does
is
it
ends
the
worker
thread
and
returns.
A
promise
that
resolves
to
the
returned
could
be
exit
code
of
the
work
of
the
of
the
worker
thread.
If
we,
if
we
have
cleanup
code
or
whatever,
and
we
want
to
make
sure
the
worker
threads
I
could
cleanly,
we
can
put
this
in
an
async
function
and
wait
the
value,
but
in
this
case
we
don't
I'm
going
to
exit
with
an
error
code.
A
You
know
it's
gonna
exit
with
Medicare
code
because
we're
terminating
it
while
it's
running,
we
could
also
send
it
a
message
and
have
it
end
gracefully,
but
that's
that's
extra
code
and
overhead.
We
don't
need
in
this
case
this
just
says
an
execution
immediately,
please,
but
we
could
do
something
more
elegant
if
we
wanted
to.
And
lastly,
we
print
our
results.
So,
let's
see
how
this
performs.
Remember:
single
threaded,
breadth-first
search
took
over
14
seconds
single
threaded,
bi-directional
search
under
three
seconds.
A
Oh
my
gosh,
it's
less
than
seven
hundred
milliseconds
now
I
can't
believe
it
unbelievable.
This
should
be
illegal.
Now.
I
have
to
admit
that
the
main
motivation
here
was
is
you
might
have
picked
up
by
now,
wasn't
really
to
talk
about
worker
threads
as
awesome
as
they
are
and
as
exciting
as
they
are.
It
was
to
write
a
program
to
efficiently
find
out
how
far
pals
family
Steakhouse
is
from
little
nests
X.
The
answer
by
the
way
is
six
degrees.
A
It
starts
with
a
little
Ness
X,
of
course,
and
the
first
degrees
Billy
Ray
Cyrus,
who
you
performed
on
an
old
town.
Road
I
was
as
surprised
as
anybody
to
find
out
that
as
recently
as
2009,
that's
what
Billy,
Ray
Cyrus
looked
like.
The
second
degree
is
country
star,
Mary,
Chapin
carpenter,
who,
along
with
Billy
Ray
Cyrus,
was
on
Dolly
Parton
song
Romeo,
mad
Dolly
Parton
gets
her
own
slide
because
you
no
need
to
stop
and
just
pay
if
he
respects
the
Dolly
Parton.
She,
the
affirmation,
Romeo,
was
on
her
30
second
studio,
album.
A
She
wrote
it.
She
produced
it.
People
who
aren't
country,
music
fans
and
I'm
I'm,
not
really
one
myself,
but
don't
realize
the
extent
to
which
he
was
in
control
of
her
sound
in
her
career.
She's
a
legend
and
a
force
to
be
reckoned
with,
so
don't
mess
with
dolly.
Also
this
you
know
since
we're
starting
from
Old,
Town
Road
and
everything
like
that,
but
the
analogous
legend
and
force
to
be
reckoned
with
and
note
is
Anna
Henningsen
she's,
the
one
most
responsible
for
implementing
worker
threats.
A
Basically
did
it
single-handedly
as
far
as
all
things
node
go
it's
extremely
difficult
to
give
on
too
much
credit.
So
you
know
you
should
totally
just
like
start
applauding
right
now.
Yeah,
that's
right!
Show
me
she'll,
be
one
of
the
people
on
the
node
technical
steering
committee
panel
at
9:00
a.m.
tomorrow
morning,
so
check
that
out
anyway,
back
to
this
nonsense,
after
Dolly
Parton's
track,
Mary
Chapin
carpenter
goes
through
Saturday
Night,
Live
band
leader,
G
Smith
and
Tom
Waits,
and
trumpet
blare
named
Chris
Grady
Flynn
on
track.
That
I
was
on
anyway.
A
Why
should
I
restrict
the
fun
of
vanity
exercises
like
this
to
me?
You
can
head
over
to
this
glitch
URL
and
try
some
stuff
out
and
since
you
know,
I
do
want
to
just
take
a
okay.
Let's
see
here.
This
is
not
working.
The
way
I
expected
it
okay,
see
here
yeah,
so
we
can
yeah.
So,
let's
see
here,
you
know
no,
let's
try
a
did
come
at
the
Frog,
because
my
daughter,
it
doesn't
matter
why
I
did
cover
the
Frog
earlier.
A
I
did
cover
for
the
Bob
Dylan,
but
that
that
that
didn't
seem
as
much
fun
as
some
other
things.
What
the
Pope
the
Pope
is
yeah
musician,
I,
don't
think
he's
in
the
discography
but
I.
Don't
we
could
try
like
Miles
Davis
or
something
I'll.
Try
that
here
you
go
four
steps
from
Kermit
the
Frog
to
Miles
Davis.
So
anyway,
you
know
you
search
for
people
and
they're,
not
and
oh-
and
this
is
where
the
visualizer
comes
in.
A
So
you
know,
here's
here's
Herbie,
Hancock
and
everybody
he's
played
with,
and
then
you
know
if
you
want
to
know
what
Randy
Kerberos
well,
he
was
on
this
Herbie
Hancock
thing,
but
it's
you
know.
But
if
you
want
to
everything,
Herbie
Hancock
is
there.
It
is
you
know
you
get
the
idea.
Okay,
so
anyway,
we're
now
so
have
fun
with
that
that
glitch
thing
yay,
okay,
present,
please.
This
is
gonna.
A
Throw
me
back
to
the
beginning:
don't
throw
me
back
to
the
beginning:
yeah,
okay,
so
there
you
go,
there's
also
a
link
at
past
mistakes,
calm,
Thanks,
I!
Think,
oh,
my
gosh
I
came
in
like
four
minutes
under
that's
awesome:
okay,
yeah
Wow,
everybody
stayed,
it
seemed
like
and
that's
just
fun,
I
hope,
I
hope,
I,
don't
didn't
completely
waste
your
time!
Thank
you
very
much.