Skip to content

perf: Remove obsolete IE8 computedStyle fallback in getBoundingClientRect wrapper #9149

@PascalThuet

Description

@PascalThuet

Description

The getBoundingClientRect() wrapper in dom.js includes a fallback that calls computedStyle() when height or width are missing from the native getBoundingClientRect() result. This fallback was added for IE8 compatibility, which is no longer supported.

Current Code (dom.js:456-477)

export function getBoundingClientRect(el) {
  if (el && el.getBoundingClientRect && el.parentNode) {
    const rect = el.getBoundingClientRect();
    const result = {};

    ['bottom', 'height', 'left', 'right', 'top', 'width'].forEach(k => {
      if (rect[k] !== undefined) {
        result[k] = rect[k];
      }
    });

    // IE8 fallback - no longer needed
    if (!result.height) {
      result.height = parseFloat(computedStyle(el, 'height'));
    }
    if (!result.width) {
      result.width = parseFloat(computedStyle(el, 'width'));
    }

    return result;
  }
}

Proposed Fix

Simplify to direct property access (all modern browsers provide these properties):

export function getBoundingClientRect(el) {
  if (el && el.getBoundingClientRect && el.parentNode) {
    const rect = el.getBoundingClientRect();
    return {
      bottom: rect.bottom,
      height: rect.height,
      left: rect.left,
      right: rect.right,
      top: rect.top,
      width: rect.width
    };
  }
}

Benchmark Results

Metric Current Optimized Improvement
10,000 calls 15.90 ms 3.20 ms 80% faster
Per call 1.59 µs 0.32 µs -1.27 µs

Impact

This function is called frequently during:

  • Seek bar scrubbing: ~30 times/second via getPointerPosition()
  • Volume control interaction
  • Tooltip positioning

On mobile devices (4x slower JS execution), this translates to ~5ms/sec savings during scrubbing.

Browser Support

All browsers currently supported by video.js provide height and width from getBoundingClientRect():

  • Chrome 1+
  • Firefox 3+
  • Safari 4+
  • Edge (all versions)

IE8 support was dropped years ago.

Environment

  • video.js version: 8.23.6
  • Tested on: Chrome 144, macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions