Compare two DOMRects for size and position match

Compare x, y, width and height exactly.

JavaScript Version 1.0.0 · MIT · .md

compare-two-dom-rects-for-size-and-position-match.js
function sameRect({ a, b }) { return ['x', 'y', 'width', 'height'].every(key => a[key] === b[key]); }

Input & expected output

Input

{
  "a": {
    "x": 0,
    "y": 0,
    "width": 100,
    "height": 200
  },
  "b": {
    "x": 0,
    "y": 0,
    "width": 100,
    "height": 200
  }
}

Expected output

true

The edge case

Input

{
  "a": {
    "x": 0,
    "y": 0,
    "width": 100,
    "height": 200
  },
  "b": {
    "x": 0,
    "y": 0.5,
    "width": 100,
    "height": 200
  }
}

Expected output

false

Know the limits

  • Exact comparison. Layout coordinates are often fractional, as the edge case shows; compare rendered positions with a tolerance such as Math.abs(a - b) < 0.5.
  • The original compared top and left, which plain objects lack.

The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.

Source & maintenance

Based on Compare two DOMRects for size and position match from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Compares x and y instead of top and left so plain JSON rects work; runs in the lab. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.