Skip to main content

17 posts tagged with "javascript"

View All Tags

V8 Built-in functions

function foo() {
const x = { bar: 'bar' };
%DebugTrackRetainingPath(x);
return () => {
return x;
};
}
const closure = foo();
gc();

vvv

💃 universe [master] node --allow-natives-syntax --track-retaining-path --expose-gc src/test.js

#################################################
Retaining path for 0x33a90e9bcb89:

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Distance from root 3: 0x33a90e9bcb89 <Object map = 0x33a9d65bbf09>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Distance from root 2: 0x33a90e9bcb51 <FunctionContext[5]>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Distance from root 1: 0x33a90e9bcc09 <JSFunction (sfi = 0x33a99d8d7a89)>

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Root: (Isolate)
-------------------------------------------------

isObject()

function isObject(value): boolean %checks {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}

Jest implementation:

function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
}

Is this thing a number?

TL;DR - do not use only isNaN for this and write a lot of tests.

StackOverflow implementation (so far ✅):

function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}

Facebook implementation (❌ fails for many values like null, booleans, Date object, empty strings, ...):

function isNumber(number) {
return !isNaN(number) && isFinite(number);
}

@cookielab implementation (❌ fails for values like 7.2acdgs and it's not multiplatform):

function isNumeric(n) {
return Number.isFinite(Number.parseFloat(n));
}

Please note that isNaN and Number.isNaN differs significantly (isNaN performs a type conversion). The same for isFinite vs Number.isFinite:

In comparison to the global isFinite() function, this method doesn't forcibly convert the parameter to a number. This means only values of the type number, that are also finite, return true.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite

Number.isFinite('0'); // false, would've been true with global isFinite('0')
Number.isFinite(null); // false, would've been true with global isFinite(null)

Polyfill (to understand the difference better):

Number.isFinite =
Number.isFinite ||
function (value) {
return typeof value === 'number' && isFinite(value);
};

Getters can be dangerous

// @flow

type x = {
+address: ?{|
+fullAddress: ?string,
|},
};

class WTF {
_address = {
fullAddress: 'yay',
};

get address() {
const addr = this._address;
this._address = null;
return addr;
}
}

const y = new WTF();

// this is going to explode:
console.warn(y.address?.fullAddress && y.address.fullAddress);

// here is why:
// console.warn(
// y.address,
// y.address,
// );

source: https://github.com/facebook/flow/issues/5479#issuecomment-349749477

Unfortunatelly, Flow cannot uncover this version (which can also explode):

{
y.address && y.address.fullAddress && <Text>{y.address.fullAddress}</Text>;
}