question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Non-primitive function parameters

See original GitHub issue

The current implementation of memoize doesn’t work correctly with non-primitive function parameters.

E.g. look at this test:

it('should not overwrite non-primitive arguments', next => {
  const cache = Object.create(null)
  let c = memoize((key => key), { cache })

  const key1 = { a: 1 }
  const key2 = { a: 2 }
  c(key1)
  c(key2)

  expect(Object.keys(cache)).to.have.length(2)
  // ... cache is { '[Object object]': { a: 2 } } now

  next()
});

Do you think it would make sense to print a corresponding error message when they try to pass any non-primitive ref as a parameter?

WeakMap could be a solution for this problem (for memory leak also).

cc @stryju

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
developitcommented, Jan 31, 2017

yup, all correct and all currently true. Problem is I’ve used this lib quite a bit for things under the assumption only the first argument is the cache key and others are ignored. One such example would be caching GET requests through memoize(fetch) - passing any options object would result in a failing reference equality check for the second argument.

Even with a WeakMap-implemented key setup, the following would be awkward:

m(['a', 'b']) !== m(['a', 'b'])  // seems like it ought to be true, but the arrays aren't referentially equal
1reaction
stryjucommented, Jan 31, 2017

to add to this, here’s a list of false-positive cache hits:

m(1) === m('1')
m(NaN) === m('NaN')
m([]) === m([,]) === m('')
m(['1,2', 3]) === m([1, 2, 3]) === m('1,2,3') // and so on
m(false) === m('false')
m(undefined) === m('undefined') === m(window.all) // I know, right?!..
// ... and so on

It would be “OK” assuming this would be used for functions with single-type param


on a different note, memoize() will not work properly for functions with more than one param

Read more comments on GitHub >

github_iconTop Results From Across the Web

Primitive Values vs. Non-Primitive Values in JavaScript
In JavaScript, objects and functions are considered non-primitive values. You'll sometimes hear non-primitive values referred to as:.
Read more >
Java Non-Primitive Data Types - W3Schools
Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot. A primitive type has always a value,...
Read more >
Data Types in Java | Primitive and Non-Primitive Data Types
Primitive Data Types: A primitive data type is pre-defined by the programming language. The size and type of variable values are specified, and ......
Read more >
Wrapping C++ function with arguments of non-primitive data ...
This is a real function from RocksDB C++ API . In order to use it in Go , it should be wrapped in...
Read more >
Non-primitive Data Types in Java | Scaler Topics
Upon creating an object of class Demo, we have invoked two functions with some parameters to perform addition and subtraction and the result ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found