# P5() instance inside p5.Vector?

**URL:** <https://discourse.processing.org/t/p5-instance-inside-p5-vector/19330>\
**Category:** Beginners\
**Created:** [April 2, 2020, 9:42am UTC](https://discourse.processing.org/t/p5-instance-inside-p5-vector/19330 "2020-04-02T09:42:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcg](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@marcg](https://discourse.processing.org/u/marcg)\
**Post date:** [April 2, 2020, 9:42am UTC](https://discourse.processing.org/t/p5-instance-inside-p5-vector/19330/1 "2020-04-02T09:42:22Z")

</div>

Hi there. Recently I tried optimising a project I have and I noticed something strange. The p5.Vector() object contains an instance of p5()… the whole p5() object  
Inside the src code I found this

```auto
p5.Vector = function Vector() {
  let x, y, z;
  // This is how it comes in with createVector()
  if (arguments[0] instanceof p5) {
    // save reference to p5 if passed in
    this.p5 = arguments[0];
    x = arguments[1][0] || 0;
    y = arguments[1][1] || 0;
    z = arguments[1][2] || 0;
    // This is what we'll get with new p5.Vector()
  } else {
    x = arguments[0] || 0;
    y = arguments[1] || 0;
    z = arguments[2] || 0;
  }

```

Fair enough, maybe for some functionality you would need an instance of p5 inside the Vector(). But my Vectors were created without the first argument being a p5(), but the instance is still there! Demo [here](https://editor.p5js.org/marcgr9/sketches/2p3KI4zYr)

The size of a p5() object is [202 bytes](https://stackoverflow.com/questions/1248302/how-to-get-the-size-of-a-javascript-object), which seems like a lot of extra memory for each Vector(). Is there a real need for this? I find it odd that I can

```auto
createVector().p5.createVector().p5.createVector().......

```

---

<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:** [April 2, 2020, 2:03pm UTC](https://discourse.processing.org/t/p5-instance-inside-p5-vector/19330/2 "2020-04-02T14:03:56Z")

</div>

> [@marcg](#):
>
> the whole p5() object

Variables & properties don’t store an entire object but only hold its reference value (a.K.a. pointer or memory address).

By itself, a property holding a reference won’t increase its object’s size much but just a tiny bit enough to store the memory address reference.

> [@marcg](#):
>
> , maybe for some functionality you would need an instance of p5 inside the Vector().

Some p5.Vector methods takes p5’s **angleMode()** into consideration:

> **[reference | p5.js](https://p5js.org/reference/#/p5/angleMode)**
>
> 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:** ![marcg](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@marcg](https://discourse.processing.org/u/marcg)\
**Post date:** [April 2, 2020, 2:58pm UTC](https://discourse.processing.org/t/p5-instance-inside-p5-vector/19330/3 "2020-04-02T14:58:51Z")

</div>

> [@GoToLoop](#):
>
> > [@marcg](#):
> >
> > the whole p5() object
> 
> Variables & properties don’t store an entire object but only hold its reference value (a.K.a. pointer or memory address).
> 
> By itself, a property holding a reference won’t increase its object’s size much but just a tiny bit enough to store the memory address reference.

Wouldn’t that be true only if p5() was some kind of a singleton?  
Check [this](https://editor.p5js.org/marcgr9/sketches/G6Rn5b8oD) out

---

<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:** [April 2, 2020, 3:18pm UTC](https://discourse.processing.org/t/p5-instance-inside-p5-vector/19330/4 "2020-04-02T15:18:38Z")

</div>

> [@marcg](#):
>
> Wouldn’t that be true only if p5() was some kind of a singleton?

A variable or property can hold 1 value only.  
If there’s more than 1 p5 running, a p5.Vector can only refer to 1 of them.

---

<div class="post-metadata">

**Author:** ![marcg](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@marcg](https://discourse.processing.org/u/marcg)\
**Post date:** [April 2, 2020, 7:02pm UTC](https://discourse.processing.org/t/p5-instance-inside-p5-vector/19330/5 "2020-04-02T19:02:56Z")

</div>

I got it, thank you! Since p5’s memory reference is never modified, in p5.Vector() there is just a reference to it, not an instance.
