b2World::DrawDebugDataInAABB

If your DrawDebugData is very slow because your level is large, this version seems to operate at a much higher performance.

Code:
   public function DrawDebugDataInAABB(screenAABB:b2AABB):void {

      if (m_debugDraw == null)
      {
         return;
      }

      m_debugDraw.m_sprite.graphics.clear();

      var flags:uint = m_debugDraw.GetFlags();

      var i:int;
      var b:b2Body;
      var f:b2Fixture;
      //var s:b2Shape;
      var j:b2Joint;
      var bp:IBroadPhase;
      var invQ:b2Vec2 = new b2Vec2;
      var x1:b2Vec2 = new b2Vec2;
      var x2:b2Vec2 = new b2Vec2;
      var xf:b2Transform;
      var b1:b2AABB = new b2AABB();
      var b2:b2AABB = new b2AABB();
      var vs:Array = [new b2Vec2(), new b2Vec2(), new b2Vec2(), new b2Vec2()];

      // Store color here and reuse, to reduce allocations
      var color:b2Color = new b2Color(0, 0, 0);

      if (flags & b2DebugDraw.e_shapeBit)
      {
         
         function renderCallback(f:b2Fixture):Boolean {
            var b:b2Body = f.GetBody();
            var s:b2Shape = f.GetShape();
            var xf:b2Transform = b.m_xf;

            if (b.IsActive() == false)
            {
               color.Set(0.5, 0.5, 0.3);
               DrawShape(s, xf, color);
            }
            else if (b.GetType() == b2Body.b2_staticBody)
            {
               color.Set(0.5, 0.9, 0.5);
               DrawShape(s, xf, color);
            }
            else if (b.GetType() == b2Body.b2_kinematicBody)
            {
               color.Set(0.5, 0.5, 0.9);
               DrawShape(s, xf, color);
            }
            else if (b.IsAwake() == false)
            {
               color.Set(0.6, 0.6, 0.6);
               DrawShape(s, xf, color);
            }
            else
            {
               color.Set(0.9, 0.7, 0.7);
               DrawShape(s, xf, color);
            }
            
            return true;
         }

         QueryAABB(renderCallback, screenAABB);
         
         /*for (b = m_bodyList; b; b = b.m_next)
         {
            xf = b.m_xf;
            for (f = b.GetFixtureList(); f; f = f.m_next)
            {
               s = f.GetShape();
               if (b.IsActive() == false)
               {
                  color.Set(0.5, 0.5, 0.3);
                  DrawShape(s, xf, color);
               }
               else if (b.GetType() == b2Body.b2_staticBody)
               {
                  color.Set(0.5, 0.9, 0.5);
                  DrawShape(s, xf, color);
               }
               else if (b.GetType() == b2Body.b2_kinematicBody)
               {
                  color.Set(0.5, 0.5, 0.9);
                  DrawShape(s, xf, color);
               }
               else if (b.IsAwake() == false)
               {
                  color.Set(0.6, 0.6, 0.6);
                  DrawShape(s, xf, color);
               }
               else
               {
                  color.Set(0.9, 0.7, 0.7);
                  DrawShape(s, xf, color);
               }
            }
         }*/
      }

      if (flags & b2DebugDraw.e_jointBit)
      {
         for (j = m_jointList; j; j = j.m_next)
         {
            DrawJoint(j);
         }
      }

      if (flags & b2DebugDraw.e_controllerBit)
      {
         for (var c:b2Controller = m_controllerList; c; c = c.m_next)
         {
            c.Draw(m_debugDraw);
         }
      }

      if (flags & b2DebugDraw.e_pairBit)
      {
         color.Set(0.3, 0.9, 0.9);
         for (var contact:b2Contact = m_contactManager.m_contactList; contact; contact = contact.GetNext())
         {
            var fixtureA:b2Fixture = contact.GetFixtureA();
            var fixtureB:b2Fixture = contact.GetFixtureB();

            var cA:b2Vec2 = fixtureA.GetAABB().GetCenter();
            var cB:b2Vec2 = fixtureB.GetAABB().GetCenter();

            m_debugDraw.DrawSegment(cA, cB, color);
         }
      }

      if (flags & b2DebugDraw.e_aabbBit)
      {
         bp = m_contactManager.m_broadPhase;

         vs = [new b2Vec2(),new b2Vec2(),new b2Vec2(),new b2Vec2()];

         for (b= m_bodyList; b; b = b.GetNext())
         {
            if (b.IsActive() == false)
            {
               continue;
            }
            for (f = b.GetFixtureList(); f; f = f.GetNext())
            {
               var aabb:b2AABB = bp.GetFatAABB(f.m_proxy);
               vs[0].Set(aabb.lowerBound.x, aabb.lowerBound.y);
               vs[1].Set(aabb.upperBound.x, aabb.lowerBound.y);
               vs[2].Set(aabb.upperBound.x, aabb.upperBound.y);
               vs[3].Set(aabb.lowerBound.x, aabb.upperBound.y);

               m_debugDraw.DrawPolygon(vs, 4, color);
            }
         }
      }

      if (flags & b2DebugDraw.e_centerOfMassBit)
      {
         for (b = m_bodyList; b; b = b.m_next)
         {
            xf = s_xf;
            xf.R = b.m_xf.R;
            xf.position = b.GetWorldCenter();
            m_debugDraw.DrawTransform(xf);
         }
      }
   }
This is a great example of how to use the AABB query function.

Original thread: https://box2d.org/forum/viewtopic.php?t=5720