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
3 years ago
14

Help

Mathematics
1 answer:
Ann [662]3 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
Brainliest!! The missing inequality is
Usimov [2.4K]

Answer:

please be more specific

Step-by-step explanation:

8 0
3 years ago
PLS PLS PLS HELP IM STUCK AND I KEEP GETTING IT WRONG
Reil [10]

Answer:  burh

hey i know the answer but

Step-by-step explanation:

3 0
3 years ago
Nine less than the product of 6 and a number is equal to 8
Romashka-Z-Leto [24]

Answer:

2.83

Step-by-step explanation:

You want to add 9 to 8 to get 17. 6 times a number must therefore equal 17. You can use the opposite which is division and divide 17 by 6 to get 2.83.

8 0
3 years ago
Elena is making greeting cards, which she will sell by the box at an arts fair. She paid $54 for
TiliK225 [7]

Answer:

Elena's sales and expenses will both be $

396

if she sells

36

boxes of cards.

Step-by-step explanation:

7 0
3 years ago
A doctor has found that the number of hours students sleep each night is inversely related to the amount of time they spend play
natita [175]

Answer: 6.25

Step-by-step explanation:

4 0
4 years ago
Other questions:
  • Can someone please solve 5-2x&lt;7
    10·1 answer
  • 1. Given: BJ≅ CF
    11·2 answers
  • Y=− 2 x + 7 ; y = 5 x − 7
    13·1 answer
  • A player in a board game will roll two standard dice. She needs to roll a 9, 10, or 11 to win the game. Determine the sample spa
    12·1 answer
  • I need help with this and I don’t understand it
    13·1 answer
  • Please respond asap! If its right i'll mark brainliest
    9·2 answers
  • Sally earns $12.50 an hour cleaning houses. If she works from 8:00am to 6:00pm, how much money will she make?
    13·2 answers
  • Free VBUCKS!!!! GANG GANG
    15·2 answers
  • I NEEEED HELP ON THIS FAST!!!
    8·2 answers
  • Question 13 (2 points)Find the area of the Sector of a circle with the given angle. Round your answer to the nearest tenth.3158
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!