1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ioda
2 years ago
14

Help

Mathematics
1 answer:
Ann [662]2 years ago
7 0

Step-by-step explanation:

ES6 has a new Set data structure for storing sets of unique objects. However it is based on object references as opposed to value comparisons. As far as I can tell this makes it impossible to have a set of pairs of numbers without stringifying.

For example, typing in Chrome's console (needs Chrome 38+):

> var s = new Set(); < undefined > s.add([2, 3]); < Set {[2, 3]} > s.has([2, 3]) < false <--- was hoping for 'true'

This appears to be by design: since I passed a different array of [2, 3] to has(), it returns false, because although the contents is the same it only looks at object references, and I allocated a new and different array to pass to has(). I would need to store a reference to the original array I passed to add() to check with has(), but this is not always possible. For example if the number pairs represent co-ordinates, I might need to check if the set has [obj.x, obj.y], but this will always return false since it allocates a new array.

The workaround is to stringify the arrays and key on strings like "2, 3" instead. However in something performance-sensitive like a game engine, it is unfortunate if every set access needs to make a string allocation and convert and concatenate number strings.

Does ES6 provide any feature to solve this problem without stringifying, or is there any feature on the horizon with ES7 that could help as well?

javascriptperformancedata-structuressetecmascript-6

Share

2

Follow

asked Sep 21 '14 at 13:06



AshleysBrain

20.4k1515 gold badges8181 silver badges119119 bronze badges

Add a comment

3 Answers

ActiveOldestVotes

3

As you've noted [2, 3] === [2, 3] is false, meaning you can't use Set like this; however, is Set really the best option for you?

You may find that using a two-level data structure like this will be better for you

var o = {}; function add(o, x, y) { if (!o[x]) o[x] = {}; o[x][y] = true; } function has(o, x, y) { return !!(o[x] && o[x][y]); } function del(o, x, y) { if (!o[x]) return; delete o[x][y]; // maybe delete `o[x]` if keys.length === 0

You might be interested in
A rectangular pool is twice as
dedylja [7]

Step-by-step explanation:

L=2B

P=2(L+B)

48=2(2B+B)

48=2(3B)

48=6B

B=8yd

L=2*8

=16yd

8 0
3 years ago
Read 2 more answers
Lindy puts £ 78 into an account which pays inrerest at a rate of 5% per anum. What will be the simple interest after 6 years .wh
Alika [10]

Answer:

Interest: £23.4

Amount: £101.4

Step-by-step explanation:

6 × 5/100 × 78 = 23.4

Balance: 78 + 23.4 = 101.4

5 0
2 years ago
Read 2 more answers
Meh needs some help once again
Ymorist [56]
1:1 because you simplify
7 0
2 years ago
A number that multiplies a variable in a term is a _-
Leto [7]
It is called a variable
8 0
3 years ago
Read 2 more answers
Is there a easy way to subtract fractions with unlike deiomaters
Angelina_Jolie [31]

Answer:

Yes

Step-by-step explanation:

It's just like adding fractions where you have to find the common denominator so the fractions can be subtracted.

<u>Example</u>

<u />\displaystyle \frac{1}{2}-\frac{1}{3}=\frac{1*3}{2*3}-\frac{2*1}{2*3}=\frac{3}{6}-\frac{2}{6}=\frac{1}{6}

8 0
2 years ago
Other questions:
  • bob loves his cows. He has 53 cows on his farm. Bob spends 10 minutes every day petting each cow. He spends 200 minutes twice a
    6·1 answer
  • If a dolphin is at -7 meters below sea level and jumps 0.5 meters, wheres the dolphin at now?
    9·1 answer
  • Which is f(5) for the function –2x2 + 2x – 3?
    13·2 answers
  • WHAT IS ANOTHER WAY TO EXPRESS 52+24
    7·1 answer
  • Marty is explaining to Susan how to solve the following problem, -4 – (-5)
    10·1 answer
  • Rearrange the equation below to solve for y.<br> 6x+6y= 24
    8·2 answers
  • Find the sum of the first 20 terms
    7·1 answer
  •  student found the slope of a line that passes through the points (1, 14)and (3, 4) to be 5. What mistake did she make?
    6·1 answer
  • PLEASE HELP <br><br>ONLY ANSWER IF YOU KNOW ​
    10·2 answers
  • 9 is substracted from the product of 7 and 3​
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!