Run details:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Chrome 61
Linux
Other
7 years ago
Test name Executions per second
canBeAddedToWeakSet_switch_simple 379.4 Ops/sec
canBeAddedToWeakSet_switch_htmlallcollection 179.1 Ops/sec
canBeAddedToWeakSet_from_es6_weakset 415.9 Ops/sec
canBeAddedToWeakSet_3eq_typeof_x2 410.8 Ops/sec
canBeAddedToWeakSet_2eq_typeof_x2 365.0 Ops/sec
canBeAddedToWeakSet_implicit_typeof_x2 346.6 Ops/sec
canBeAddedToWeakSet_boolean_typeof_x2 344.2 Ops/sec
canBeAddedToWeakSet_try_catch 5.5 Ops/sec
canBeAddedToWeakSet_short_circuit_typeof 426.3 Ops/sec
Script Preparation code:
x
 
var DATA = (function (K, N, total) {
    function random(n) {
        return Math.floor(Math.random() * 7);
    }
    function randomKey() {
        return Math.random().toString(36).slice(2);
    }
    function randomCollection(count = random(N)) {
        for (var a = new Array(count), i = 0; i < count; i++) {
            a[i] = randomValue();
        }
        return a;
    }
    function randomObject(keysCount = random(N)) {
        for (var obj = {}, i = 0; i < keysCount; i++) {
            obj[randomKey()] = randomValue();
        }
        return obj;
    }
    function randomValue(type = random(K)) {
      switch (type % K) {
        case 0: return undefined;
        case 1: return null; break;
        case 2: return String(Math.random());
        case 3: return Math.random() > 0.5;
        case 4: return Math.random() * 1E3;
        case 5: return randomCollection();
        default: return randomObject();
      }
    }
    return randomCollection(total);
}(7, 5, 1E5));
function canBeAddedToWeakSet_switch_simple(value) {
  switch (typeof value) {
    case 'undefined':
    case 'boolean':
    case 'number':
    case 'string':
    case 'symbol':
      return false;
    case 'object':
      return value !== null
    default:
      return true;
  }
}
function canBeAddedToWeakSet_switch_htmlallcollection(value) {
  switch (typeof value) {
    case 'undefined':
      return value !== undefined
    case 'boolean':
    case 'number':
    case 'string':
    case 'symbol':
      return false
    case 'object':
      return value !== null
    default:
      return true
  }
}
function canBeAddedToWeakSet_from_es6_weakset(value) {
    return typeof value === 'object' ? value !== null : typeof value === 'function';
}
function canBeAddedToWeakSet_short_circuit_typeof(value) {
    return typeof value === 'object' && value !== null || typeof value === 'function';
}
function canBeAddedToWeakSet_3eq_typeof_x2(value) {
    return value !== null && (typeof value === 'object' || typeof value === 'function');
}
function canBeAddedToWeakSet_2eq_typeof_x2(value) {
    return value != null && (typeof value === 'object' || typeof value === 'function');
}
function canBeAddedToWeakSet_implicit_typeof_x2(value) {
    return value && (typeof value === 'object' || typeof value === 'function');
}
function canBeAddedToWeakSet_boolean_typeof_x2(value) {
    return !!value && (typeof value === 'object' || typeof value === 'function');
}
const _set_ = new WeakSet();
function canBeAddedToWeakSet_try_catch(value) {
    try { _set_.add(value); return true; }
    catch (e) { return false; }
}
function test(canAdd) {
    const count = DATA.reduce((countOfAddables, value) => (canAdd(value) ? 1 : 0) + countOfAddables, 0);
    console.log('can add %d out of %d values in the array', count, DATA.length);
}
Tests:
  • canBeAddedToWeakSet_switch_simple

     
    test(canBeAddedToWeakSet_switch_simple);
  • canBeAddedToWeakSet_switch_htmlallcollection

     
    test(canBeAddedToWeakSet_switch_htmlallcollection);
  • canBeAddedToWeakSet_from_es6_weakset

     
    test(canBeAddedToWeakSet_from_es6_weakset);
  • canBeAddedToWeakSet_3eq_typeof_x2

     
    test(canBeAddedToWeakSet_3eq_typeof_x2);
  • canBeAddedToWeakSet_2eq_typeof_x2

     
    test(canBeAddedToWeakSet_2eq_typeof_x2);
  • canBeAddedToWeakSet_implicit_typeof_x2

     
    test(canBeAddedToWeakSet_implicit_typeof_x2);
  • canBeAddedToWeakSet_boolean_typeof_x2

     
    test(canBeAddedToWeakSet_boolean_typeof_x2);
  • canBeAddedToWeakSet_try_catch

     
    test(canBeAddedToWeakSet_try_catch);
  • canBeAddedToWeakSet_short_circuit_typeof

     
    test(canBeAddedToWeakSet_short_circuit_typeof);