Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

deleted 37 characters in body
Source Link
Kamil Kiełczewski
  • 93.8k
  • 34
  • 402
  • 375

Today 20202023.013.17,20 I performedperform tests for chosen solutions on macOS High Sierra 10.13MacOs Monterey 12.61 (M1, 16GB) on Chrome v79.0v109, Safari v13.0v15.4,2 and Firefox v72.0; for the chosen solutionsv110.

  • Solutions based on for-in (A, J, L, M) are fast or fastest on all browsers
  • SolutionsSolution based on JSON.stringify (B, K) are slow
  • Surprisingly, the solution based on Object (N) is also slow
  • NOTE: This table does not match the photo below.slowest on all browsers

enter image description here enter image description here

There are 15 solutions are presented in the snippet below. If you want to run a performance test on your machine, clickHERE . This link was updated 2021年07月08日, but

  • HERE for empty object
  • HERE for object witch 10 fields
  • HERE for object with 100 fields

Old version of this answer contains tests originally were performed here - and results in the table above came from there2020 - (but now it looks like that service no longer works)HERE.

enter image description here enter image description here

Today 2020.01.17, I performed tests on macOS High Sierra 10.13.6 on Chrome v79.0, Safari v13.0.4, and Firefox v72.0; for the chosen solutions.

  • Solutions based on for-in (A, J, L, M) are fastest
  • Solutions based on JSON.stringify (B, K) are slow
  • Surprisingly, the solution based on Object (N) is also slow
  • NOTE: This table does not match the photo below.

enter image description here

There are 15 solutions presented in the snippet below. If you want to run a performance test on your machine, clickHERE . This link was updated 2021年07月08日, but tests originally were performed here - and results in the table above came from there (but now it looks like that service no longer works).

enter image description here

Today 2023.3.20 I perform tests for chosen solutions on MacOs Monterey 12.1 (M1, 16GB) on Chrome v109, Safari v15.2 and Firefox v110.

  • Solutions based on for-in (A, L) are fast or fastest on all browsers
  • Solution based on JSON.stringify (B) is slowest on all browsers

enter image description here

There solutions are presented in the snippet below. If you want to run a performance test on your machine, click

  • HERE for empty object
  • HERE for object witch 10 fields
  • HERE for object with 100 fields

Old version of this answer contains tests from 2020 - HERE.

enter image description here

deleted 98 characters in body
Source Link
Kamil Kiełczewski
  • 93.8k
  • 34
  • 402
  • 375

Links to answers: A , A , A, B, C, D, E, F, G, H, I, J, K, N , L,M, L,M M , N, O P Q

Links to answers: A , A , A, B, C, D, E, F, G, I, J, K, N , L,M, L,M, O P Q

Links to answers: A, B, C, D, E, F, G, H, I, J, K, L, M , N, O P Q

added 181 characters in body
Source Link
Kamil Kiełczewski
  • 93.8k
  • 34
  • 402
  • 375

Links to answers: A, A, A, B, C, D, E, F, G, I, J, K, N, L,M, L,M, O P Q

var log = (s, f) => console.log(`${s} --> {}:${f({})} {k:2}:${f({ k: 2 })}`);
function A(obj) {
 for (var i in obj) return false;
 return true;
}
function B(obj) {
 return JSON.stringify(obj) === "{}";
}
function C(obj) {
 return Object.keys(obj).length === 0;
}
function D(obj) {
 return Object.entries(obj).length === 0;
}
function E(obj) {
 return Object.getOwnPropertyNames(obj).length === 0;
}
function F(obj) {
 return Object.keys(obj).length === 0 && obj.constructor === Object;
}
function G(obj) {
 return typeof obj === "undefined" || !Object.keys(obj)[0];
}
function H(obj) {
 return Object.entries(obj).length === 0 && obj.constructor === Object;
}
function I(obj) {
 return Object.values(obj).every((val) => typeof val === "undefined");
}
function J(obj) {
 for (const key in obj) {
 if (hasOwnProperty.call(obj, key)) {
 return false;
 }
 }
 return true;
}
function K(obj) {
 var isEmpty = true;
 for (keys in obj) {
 isEmpty = false;
 break;
 }
 return isEmpty;
}
function L(obj) {
 for (var prop in obj) {
 if (obj.hasOwnProperty(prop)) return false;
 }
 return true;
}
function M(obj) {
 if (obj === null || typeof obj !== 'object' ||
 Object.prototype.toString.call(obj) === '[object Array]') {
 return false
 } else {
 for (var prop in obj) {
 if (obj.hasOwnProperty(prop)) {
 return false
 }
 }
 return JSON.stringify(obj) === JSON.stringify({})
 }
}
function N(obj) {
 return (
 Object.getOwnPropertyNames(obj).length === 0 &&
 Object.getOwnPropertySymbols(obj).length === 0 &&
 Object.getPrototypeOf(obj) === Object.prototype
 );
}
function O(obj) {
 return !(Object.getOwnPropertyNames !== undefined
 ? Object.getOwnPropertyNames(obj).length !== 0
 : (function () {
 for (var key in obj) break;
 return key !== null && key !== undefined;
 })());
}
function P(obj) {
 return $.isEmptyObject(obj)
}
function Q(obj) {
 return _.isEmpty(obj);
}
log("A", A);
log("B", B);
log("C", C);
log("D", D);
log("E", E);
log("F", F);
log("G", G);
log("H", H);
log("I", I);
log("J", J);
log("K", K);
log("L", L);
log("M", M);
log("N", N);
log("O", O);
log("P", P);
log("Q", Q);
<script
 src="https://code.jquery.com/jquery-3.6.4.min.js"
 integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8="
 crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.13.6/underscore-min.js" integrity="sha512-2V49R8ndaagCOnwmj8QnbT1Gz/rie17UouD9Re5WxbzRVUGoftCu5IuqqtAM9+UC3fwfHCSJR1hkzNQh/2wdtg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Links to answers: A, A, A, B, C, D, E, F, G, I, J, K, N, L,M, L,M, O

var log = (s, f) => console.log(`${s} --> {}:${f({})} {k:2}:${f({ k: 2 })}`);
function A(obj) {
 for (var i in obj) return false;
 return true;
}
function B(obj) {
 return JSON.stringify(obj) === "{}";
}
function C(obj) {
 return Object.keys(obj).length === 0;
}
function D(obj) {
 return Object.entries(obj).length === 0;
}
function E(obj) {
 return Object.getOwnPropertyNames(obj).length === 0;
}
function F(obj) {
 return Object.keys(obj).length === 0 && obj.constructor === Object;
}
function G(obj) {
 return typeof obj === "undefined" || !Object.keys(obj)[0];
}
function H(obj) {
 return Object.entries(obj).length === 0 && obj.constructor === Object;
}
function I(obj) {
 return Object.values(obj).every((val) => typeof val === "undefined");
}
function J(obj) {
 for (const key in obj) {
 if (hasOwnProperty.call(obj, key)) {
 return false;
 }
 }
 return true;
}
function K(obj) {
 var isEmpty = true;
 for (keys in obj) {
 isEmpty = false;
 break;
 }
 return isEmpty;
}
function L(obj) {
 for (var prop in obj) {
 if (obj.hasOwnProperty(prop)) return false;
 }
 return true;
}
function M(obj) {
 if (obj === null || typeof obj !== 'object' ||
 Object.prototype.toString.call(obj) === '[object Array]') {
 return false
 } else {
 for (var prop in obj) {
 if (obj.hasOwnProperty(prop)) {
 return false
 }
 }
 return JSON.stringify(obj) === JSON.stringify({})
 }
}
function N(obj) {
 return (
 Object.getOwnPropertyNames(obj).length === 0 &&
 Object.getOwnPropertySymbols(obj).length === 0 &&
 Object.getPrototypeOf(obj) === Object.prototype
 );
}
function O(obj) {
 return !(Object.getOwnPropertyNames !== undefined
 ? Object.getOwnPropertyNames(obj).length !== 0
 : (function () {
 for (var key in obj) break;
 return key !== null && key !== undefined;
 })());
}
log("A", A);
log("B", B);
log("C", C);
log("D", D);
log("E", E);
log("F", F);
log("G", G);
log("H", H);
log("I", I);
log("J", J);
log("K", K);
log("L", L);
log("M", M);
log("N", N);
log("O", O);

Links to answers: A, A, A, B, C, D, E, F, G, I, J, K, N, L,M, L,M, O P Q

var log = (s, f) => console.log(`${s} --> {}:${f({})} {k:2}:${f({ k: 2 })}`);
function A(obj) {
 for (var i in obj) return false;
 return true;
}
function B(obj) {
 return JSON.stringify(obj) === "{}";
}
function C(obj) {
 return Object.keys(obj).length === 0;
}
function D(obj) {
 return Object.entries(obj).length === 0;
}
function E(obj) {
 return Object.getOwnPropertyNames(obj).length === 0;
}
function F(obj) {
 return Object.keys(obj).length === 0 && obj.constructor === Object;
}
function G(obj) {
 return typeof obj === "undefined" || !Object.keys(obj)[0];
}
function H(obj) {
 return Object.entries(obj).length === 0 && obj.constructor === Object;
}
function I(obj) {
 return Object.values(obj).every((val) => typeof val === "undefined");
}
function J(obj) {
 for (const key in obj) {
 if (hasOwnProperty.call(obj, key)) {
 return false;
 }
 }
 return true;
}
function K(obj) {
 var isEmpty = true;
 for (keys in obj) {
 isEmpty = false;
 break;
 }
 return isEmpty;
}
function L(obj) {
 for (var prop in obj) {
 if (obj.hasOwnProperty(prop)) return false;
 }
 return true;
}
function M(obj) {
 if (obj === null || typeof obj !== 'object' ||
 Object.prototype.toString.call(obj) === '[object Array]') {
 return false
 } else {
 for (var prop in obj) {
 if (obj.hasOwnProperty(prop)) {
 return false
 }
 }
 return JSON.stringify(obj) === JSON.stringify({})
 }
}
function N(obj) {
 return (
 Object.getOwnPropertyNames(obj).length === 0 &&
 Object.getOwnPropertySymbols(obj).length === 0 &&
 Object.getPrototypeOf(obj) === Object.prototype
 );
}
function O(obj) {
 return !(Object.getOwnPropertyNames !== undefined
 ? Object.getOwnPropertyNames(obj).length !== 0
 : (function () {
 for (var key in obj) break;
 return key !== null && key !== undefined;
 })());
}
function P(obj) {
 return $.isEmptyObject(obj)
}
function Q(obj) {
 return _.isEmpty(obj);
}
log("A", A);
log("B", B);
log("C", C);
log("D", D);
log("E", E);
log("F", F);
log("G", G);
log("H", H);
log("I", I);
log("J", J);
log("K", K);
log("L", L);
log("M", M);
log("N", N);
log("O", O);
log("P", P);
log("Q", Q);
<script
 src="https://code.jquery.com/jquery-3.6.4.min.js"
 integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8="
 crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.13.6/underscore-min.js" integrity="sha512-2V49R8ndaagCOnwmj8QnbT1Gz/rie17UouD9Re5WxbzRVUGoftCu5IuqqtAM9+UC3fwfHCSJR1hkzNQh/2wdtg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
added 181 characters in body
Source Link
Kamil Kiełczewski
  • 93.8k
  • 34
  • 402
  • 375
Loading
added 792 characters in body
Source Link
Kamil Kiełczewski
  • 93.8k
  • 34
  • 402
  • 375
Loading
clarify mismatch between table and photo
Source Link
Akaisteph7
  • 6.9k
  • 2
  • 39
  • 53
Loading
deleted 97 characters in body
Source Link
Gershom Maes
  • 8.2k
  • 3
  • 42
  • 64
Loading
Loading
added 209 characters in body
Source Link
Kamil Kiełczewski
  • 93.8k
  • 34
  • 402
  • 375
Loading
added 84 characters in body
Source Link
Kamil Kiełczewski
  • 93.8k
  • 34
  • 402
  • 375
Loading
Loading
Source Link
Kamil Kiełczewski
  • 93.8k
  • 34
  • 402
  • 375
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /