# Why is p5.js preload so slow?

**URL:** https://discourse.processing.org/t/why-is-p5-js-preload-so-slow/12176
**Category:** Libraries
**Created:** [June 19, 2019, 7:54am UTC](https://discourse.processing.org/t/why-is-p5-js-preload-so-slow/12176 "2019-06-19T07:54:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [June 19, 2019, 7:54am UTC](https://discourse.processing.org/t/why-is-p5-js-preload-so-slow/12176/1 "2019-06-19T07:54:31Z")

</div>

I load a few mp3 files that are 2 minutes long.

```auto
function preload() {
	
	sound_dancer_1_l = loadSound('data/GH010103_1-L.mp3');
	sound_dancer_1_r = loadSound('data/GH010103_1-R.mp3');
	sound_dancer_2_l = loadSound('data/GH010103_2-L.mp3');
	sound_dancer_2_r = loadSound('data/GH010103_2-R.mp3');
	sound_dancer_3_l = loadSound('data/GH010103_3-L.mp3');
	sound_dancer_3_r = loadSound('data/GH010103_3-R.mp3');
	sound_dancer_4_l = loadSound('data/GH010103_4-L.mp3');
	sound_dancer_4_r = loadSound('data/GH010103_4-R.mp3');

}

```

It takes around 5 seconds on my localhost, which instead should have been instant.  
Is there anyway to speed it up?

If I don’t preload them and start play early on then I get an error and I have no sound at all.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 19, 2019, 3:22pm UTC](https://discourse.processing.org/t/why-is-p5-js-preload-so-slow/12176/2 "2019-06-19T15:22:11Z")

</div>

> [@clankill3r](#):
>
> If I don’t **preload()** them and start play early on then I get an error and I have no sound at all.

**preload()** is a p5js optional callback which delays the sketch until everything is processed there: 🐌

> **[reference | p5.js](https://p5js.org/reference/#/p5/preload)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

You can invoke p5::**loadSound()** outside **preload()** & check it’s ready w/ p5.SoundFile::isLoaded(): 🔉

> **[reference | p5.js](https://p5js.org/reference/#/p5.SoundFile/loadSound)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

> **[reference | p5.js](https://p5js.org/reference/#/p5.SoundFile/isLoaded)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

---

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [June 24, 2019, 2:41pm UTC](https://discourse.processing.org/t/why-is-p5-js-preload-so-slow/12176/3 "2019-06-24T14:41:31Z")

</div>

Thanks, I just used html5 audio in the end.

---

<div class="post-metadata">

### Author: ![dackdel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dackdel/32/4216_2.png) [@dackdel](https://discourse.processing.org/u/dackdel)
#### Post date: [June 29, 2019, 10:39am UTC](https://discourse.processing.org/t/why-is-p5-js-preload-so-slow/12176/4 "2019-06-29T10:39:53Z")

</div>

i got preload working with no glitches.

```auto
var myRec = new p5.SpeechRec('en-US', parseResult);
myRec.continuous = true;
myRec.interimResults = false; //was getting detection errors with this turned on, not sure what the fuck this is
myRec.onEnd = restart;
var songs = [];
var songNames = ['one.wav', 'two.m4a', 'three.m4a', 'come_closer.m4a', 'four.wav', 'five.m4a'];
var songCount = songNames.length;
var currentSong = 0;
var song;
var startT;
var fiveSeconds = 5000;

function restart() { // the dirty hack
    myRec.start();
}

function preload() {
    soundFormats('wav', 'm4a');
    for (let i = 0; i < songNames.length; i++) {
        songs.push(loadSound('audioFiles/' + songNames[i]));
    }
}

function setup() {
    frameRate(1);
    createCanvas(600, 600);
    background(255, 255, 255);
    fill(0, 0, 0, 255);
    myRec.start();
    startT = millis();
}

```
